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

后端 未结 14 2401
余生分开走
余生分开走 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 03:49

    this below code works for me in Selenium 3 and chrome version 58.

        WebDriver driver = new ChromeDriver();
        driver.get("http://yahoo.com");  
        ((JavascriptExecutor)driver).executeScript("window.open()");
        ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
        driver.switchTo().window(tabs.get(1));
        driver.get("http://google.com");
    
    0 讨论(0)
  • 2020-11-30 03:56

    You can open multiple browser or a window by using below code:

    WebDriver driver = new ChromeDriver();
    driver.get("http://yahoo.com");  
    
    WebDriver driver1 = new ChromeDriver();
    driver1.get("google.com");
    
    WebDriver driver2 = new InternetExplorerDriver();
    driver2.get("google.com/gmap");
    
    0 讨论(0)
  • 2020-11-30 03:59

    I had used the below code to open a new tab in the browser using C# selenium..

    IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
    
    js.ExecuteScript("window.open();");
    
    0 讨论(0)
  • 2020-11-30 04:03

    for clicking on the link which expected to be opened from new tab use this

    WebDriver driver = new ChromeDriver(); 
    driver.get("https://www.yourSite.com"); 
    WebElement link=driver.findElement(By.xpath("path_to_link")); 
    
    Actions actions = new Actions(driver); 
    actions.keyDown(Keys.LEFT_CONTROL) 
           .click(element) 
           .keyUp(Keys.LEFT_CONTROL) 
           .build() 
           .perform(); 
    
    ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles()); 
    driver.switchTo().window(tab.get(1));
    
    0 讨论(0)
  • 2020-11-30 04:08

    If you can get the link element you can use this. It will also take you to the tab that you have opened.

    WebElement link= driver.findElement(By.tagname("a"));  
    String keyString =   Keys.CONTROL+Keys.SHIFT.toString()+Keys.ENTER.toString());
    link.sendKeys(keyString);
    
    0 讨论(0)
  • 2020-11-30 04:08

    First open empty new Tab by using the keys Ctrl + t and then use .get() to fetch the URL you want. Your code should look something like this -

    String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
    driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
    
    driver.get("www.facebook.com");
    

    If you want to open a link on the current view in a new tab then the code you've written above can be used. Instead of By.linkText() make sure you use the appropriate By selector class to select the web element.

    0 讨论(0)
提交回复
热议问题