简体   繁体   English

如何使用Selenium Webdriver在同一窗口中打开多个选项卡(超过2个)之间切换

[英]How to switch between multiple tabs(more than 2) open in same window using Selenium Webdriver

I am learning Selenium-Webdriver and so for practice working on one scenario, but I'm stuck in step#3. 我正在学习Selenium-Webdriver,因此在一种情况下进行练习是可行的,但是我陷入了第3步。 Scenario is as follows: 方案如下:

  1. Open google homepage and perform some search, say for the word "WebDriver". 打开Goog​​le主页并执行一些搜索,例如说“ WebDriver”。
  2. Open the first two links in new tabs of the same window. 在同一窗口的新选项卡中打开前两个链接。
  3. Navigate to the second and third tab and get their titles 导航到第二个和第三个选项卡并获取其标题
  4. Close the tabs and switch back to the google result tab. 关闭标签页,然后切换回Google结果标签页。

So far, I'm able to open google home page, perform a search on the word "WebDriver" and open the first two links, but now I'm unable to switch to the second and third tab and close them. 到目前为止,我已经能够打开Goog​​le主页,对“ WebDriver”一词进行搜索并打开前两个链接,但是现在我无法切换到第二个和第三个选项卡并关闭它们。 My code so far is: 到目前为止,我的代码是:

    String originalHandle = driver.getWindowHandle();
    System.out.println("Before switching title is:" +driver.getTitle());
    String selectLinkOpeninNewTab = Keys.chord(Keys.COMMAND,Keys.ENTER);
    WebElement link1 = driver.findElement(By.xpath(".//*[@id='rso']/div[2]/div[1]/div/h3/a"));
    link1.sendKeys(selectLinkOpeninNewTab);
    WebElement link2 = driver.findElement(By.xpath(".//*[@id='rso']/div[2]/div[2]/div/h3/a"));
    link2.sendKeys(selectLinkOpeninNewTab);

    Set<String> s1 = driver.getWindowHandles();
    Iterator<String> i1 = s1.iterator();
    int i = 0;
    while(i1.hasNext())
    {
        i++;
    String childwindow = i1.next();

    if(!originalHandle.equalsIgnoreCase(childwindow))
    {
    driver.switchTo().window(childwindow);
    Thread.sleep(10000);
    System.out.println("After switching title of new Tab "+i+ " title is " +driver.getTitle());
    driver.close();
    }
}

driver.switchTo().window(originalHandle);
System.out.println("Original window tab title is" +driver.getTitle() );

I'm not sure where it's going wrong and how to fix it. 我不确定问题出在哪里以及如何解决。 :( :(

I searched for such functionality as tab switching but found nothing. 我搜索了选项卡切换等功能,但一无所获。 Closest to this is switch of windows. 最接近此窗口的是Windows。 (there are lot of comments that WindowHandles can be used for tab switching, but this is not true -- I had tried a lot. Its can be used only for windows switch, but not tabs switch) (有很多评论说WindowHandles可以用于选项卡切换,但是事实并非如此-我已经尝试了很多。它只能用于Windows切换,而不能用于选项卡切换)

if you need to open in new window -- you need to click on link with pressed shift btn 如果您需要在新窗口中打开-您需要单击带有Shift的btn链接

code is something like 代码就像

Actions.KeyDown(Keys.Shift).Click(ElementToClick).KeyUp(Keys.Shift).Build().Perform();

and if you need to switch the window 如果您需要切换窗口

var _windowsList = new List<String>(Instance.WindowHandles);
Instance.SwitchTo().Window(_windowsList[0]);

Could be too late, but hope this helps : 可能为时已晚,但希望这会有所帮助:

for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
        System.out.println(winHandle);
        driver.switchTo().window(winHandle);        // switch focus of WebDriver to the next found window handle (that's your newly opened window)              
    }

Please try with the below code: 请尝试以下代码:

Set<String> s1 = driver.getWindowHandles();
for(String childwindow : s1) {
   if(!originalHandle.equals(childwindow)) {
       driver.switchTo().window(childwindow);
       System.out.println("Tab title is " + driver.getTitle();
   }
   driver.close();
}
driver.switchTo().window(originalHandle);

Hope this helps. 希望这可以帮助。

  1. Get all window handler by below code. 通过以下代码获取所有窗口处理程序。 Set windowHandleSet = driver.getWindowHandles(); 设置windowHandleSet = driver.getWindowHandles();
  2. Loop in the window handler set, switchTo each of them. 循环进入窗口处理程序集,切换到每个。 driver.switchTo().window(windowHandleStr); driver.switchTo()。window(windowHandleStr);
  3. Run command Ctrl+number to switch between different tab in same Firefox window. 运行命令Ctrl + number可以在同一Firefox窗口的不同选项卡之间切换。 WebElement bodyEle = driver.findElement(By.tagName("body")); WebElement bodyEle = driver.findElement(By.tagName(“ body”));; bodyEle.sendKeys(Keys.CONTROL + "1"); bodyEle.sendKeys(Keys.CONTROL +“ 1”);

http://www.dev2qa.com/open-multiple-windows-tabs-in-selenium-webdriver/ http://www.dev2qa.com/open-multiple-windows-tabs-in-selenium-webdriver/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打开多个选项卡并通过Selenium和Webdriver在之间切换? - How to open multiple tabs and switch between through Selenium and Webdriver? 使用带有 Java 的 Selenium WebDriver 在浏览器选项卡之间切换 - Switch between browser tabs using Selenium WebDriver with Java 使用 Selenium WebDriver 和 Java 切换选项卡 - Switch tabs using Selenium WebDriver with Java 如何在不使用Java的Selenium Webdriver中使用页面标题的情况下切换多个窗口? - How to switch multiple window without using page title in selenium webdriver using java? 如何使用 Selenium WebDriver 打开多个窗口 - How to open multiple windows using Selenium WebDriver 如何在不使用selenium / webdriver方法的情况下切换到新窗口 - How to switch to new window without using selenium/webdriver methods 如何使用Java在Selenium WebDriver中切换帧 - How to switch between frames in Selenium WebDriver using Java 使用相同的Firefox窗口在Selenium WebDriver(Java)中运行多个测试 - Using the same Firefox Window to run multiple tests in Selenium WebDriver (Java) 如何使用带有Java的Selenium WebDriver在Chrome中的Windows之间切换? - How to switch between windows in Chrome using Selenium WebDriver with Java? 如何在Selenium WebDriver中切换回父窗口? - How to switch back to parent window in Selenium WebDriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM