[英]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: 方案如下:
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. 到目前为止,我已经能够打开Google主页,对“ 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. 希望这可以帮助。
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.