How to open a link in new tab (chrome) using Selenium WebDriver?

后端 未结 14 2402
余生分开走
余生分开走 2020-11-30 03:37
System.setProperty(\"webdriver.chrome.driver\", \"D:\\\\softwares\\\\chromedriver_win32\\\\chromedriver.exe\");

WebDriver driver = new ChromeDriver();
driver.manage         


        
相关标签:
14条回答
  • 2020-11-30 04:15

    I checked with below code and it works fine for me. I found answer from here.

        driver = new ChromeDriver();
        driver.manage().window().maximize();
                
        String baseUrl = "http://www.google.co.uk/";
        driver.get(baseUrl);
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
    
        ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
        driver.switchTo().window(tabs.get(1)); //switches to new tab
        driver.get("https://www.facebook.com");
        
        driver.switchTo().window(tabs.get(0)); // switch back to main screen        
        driver.get("https://www.news.google.com");
    
    0 讨论(0)
  • 2020-11-30 04:16

    I am trying to do a robot to my little son and just play a Youtube video and than show a robot dancing.

    For some reason, commands like CONTROL + T explained above was not working for me and maybe it is not the correct answer but I solved my problem using custom Javascript script like this:

    using (var driver = new ChromeDriver())
                {
                    var link1 = "https://www.youtube.com/watch?v=0GIgk4yuHOQ";
                    //open a music
                    driver.Navigate().GoToUrl(link1);
                    var link2 = "https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/fbe53d6d-c13f-4eec-9bcf-62f19cfab15a/d4m0h4v-9442b1f2-6a49-4818-8f51-5ebe216f043c.gif?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwic3ViIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsImF1ZCI6WyJ1cm46c2VydmljZTpmaWxlLmRvd25sb2FkIl0sIm9iaiI6W1t7InBhdGgiOiIvZi9mYmU1M2Q2ZC1jMTNmLTRlZWMtOWJjZi02MmYxOWNmYWIxNWEvZDRtMGg0di05NDQyYjFmMi02YTQ5LTQ4MTgtOGY1MS01ZWJlMjE2ZjA0M2MuZ2lmIn1dXX0.BTTlingNpBqH5O9dNVienFsArNqkfUc7KXnIgHumrBQ";
                    //Dance robot, dance
                    driver.ExecuteScript($"window.open('{link2}', '_blank');");
                    Thread.Sleep(20000);
                }
    
    0 讨论(0)
提交回复
热议问题