Ume*_*mar 61 java testing selenium automation selenium-webdriver
使用Selenium WebDriver和JAVA.我试图自动化一个功能,我必须打开一个新的选项卡在那里做一些操作,然后回到上一个选项卡(父).我使用了开关手柄,但它不起作用.还有一个奇怪的事情是两个标签有相同的窗口句柄,因此我无法在标签之间切换.
但是,当我尝试使用不同的Firefox窗口时,它可以工作,但是对于标签,它不起作用.
请帮我,如何切换标签.或者如何在不使用窗口句柄的情况下切换标签,因为在我的情况下,窗口句柄与两个标签相同.
(我观察到当你在同一个窗口中打开不同的标签时,窗口句柄保持不变)
小智 100
psdbComponent.clickDocumentLink();
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.close();
driver.switchTo().window(tabs2.get(0));
Run Code Online (Sandbox Code Playgroud)
这段代码对我来说非常有用.试试看.在要在新选项卡上执行某些操作之前,始终需要将驱动程序切换到新选项卡.
Jor*_*lva 20
这是打开新选项卡,将焦点更改为选项卡,关闭选项卡并将焦点返回到旧/原始选项卡的简单解决方案:
@Test
public void testTabs() {
driver.get("https://business.twitter.com/start-advertising");
assertStartAdvertising();
// considering that there is only one tab opened in that point.
String oldTab = driver.getWindowHandle();
driver.findElement(By.linkText("Twitter Advertising Blog")).click();
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
newTab.remove(oldTab);
// change focus to new tab
driver.switchTo().window(newTab.get(0));
assertAdvertisingBlog();
// Do what you want here, you are in the new tab
driver.close();
// change focus back to old tab
driver.switchTo().window(oldTab);
assertStartAdvertising();
// Do what you want here, you are in the old tab
}
private void assertStartAdvertising() {
assertEquals("Start Advertising | Twitter for Business", driver.getTitle());
}
private void assertAdvertisingBlog() {
assertEquals("Twitter Advertising", driver.getTitle());
}
Run Code Online (Sandbox Code Playgroud)
小智 12
Web驱动程序处理不同窗口以及它如何处理不同选项卡有所不同.
案例1:
如果有多个窗口,则以下代码可以提供帮助:
//Get the current window handle
String windowHandle = driver.getWindowHandle();
//Get the list of window handles
ArrayList tabs = new ArrayList (driver.getWindowHandles());
System.out.println(tabs.size());
//Use the list of window handles to switch between windows
driver.switchTo().window(tabs.get(0));
//Switch back to original window
driver.switchTo().window(mainWindowHandle);
Run Code Online (Sandbox Code Playgroud)
情况2:
如果同一窗口中有多个选项卡,则只有一个窗口句柄.因此,在窗口句柄之间切换可将控件保持在同一选项卡中.
在这种情况下,使用Ctrl +\t(Ctrl + Tab)在标签之间切换更有用.
//Open a new tab using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Switch between tabs using Ctrl + \t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
Run Code Online (Sandbox Code Playgroud)
详细的示例代码可以在这里找到:http:
//design-interviews.blogspot.com/2014/11/switching-between-tabs-in-same-browser-window.html
解决
假设:通过点击您网页上的内容,可以打开一个新标签.
使用以下逻辑切换到第二个选项卡.
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();
Run Code Online (Sandbox Code Playgroud)
以同样的方式,您可以再次切换回第一个标签.
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD1).build().perform();
Run Code Online (Sandbox Code Playgroud)
由于driver.window_handles
不在 order 中,因此更好的解决方案是 this。
首先使用快捷方式切换到第一个选项卡
Control + X
以切换到浏览器窗口中的第 'x' 个选项卡。
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "1");
# goes to 1st tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "4");
# goes to 4th tab if its exists or goes to last tab.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
230464 次 |
最近记录: |