如何使用Selenium WebDriver与Java切换到另一个选项卡
我打开google.com,然后点击“GMail”超链接,在同一浏览器上打开一个新标签。
现在我想切换到使用Selenium WebDriver打开GMail的新选项卡。
代码段是:
WebDriver wd = new ChromeDriver(); wd.get("https://www.google.co.in/?gws_rd=ssl"); wd.findElement(By.linkText("Gmail")).sendKeys(Keys.CONTROL,Keys.RETURN);
现在我想去我打开GMail链接的选项卡。 我已经搜索了N个解决方案,但没有一个有效。
例如
解决方案1:
String Tab1 = wd.getWindowHandle(); ArrayList availableWindows = new ArrayList(wd.getWindowHandles()); if (!availableWindows.isEmpty()) { wd.switchTo().window(availableWindows.get(1)); }
解决方案2:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
请建议。 我坚持这个。
在我看来,driver.getWindowHandles()不适用于标签…你总是会使用CURRENT标签的句柄返回一个项目数组… [删除旧答案,因为这似乎现在已修复]
更新(2016年6月16日):当前版本的Selenium(服务器独立2.53.0)似乎采取了不同的行动。 现在我正在使用这样的东西打开/切换驱动程序到新选项卡:
更新(2016年11月16日):Selenium 3.0.1似乎又改变了一些东西? 我现在必须使用javascript打开一个新标签。 这似乎只适用于Chrome … Firefox打开一个新窗口。 我将看看是否可以使用geckodriver的设置(function/配置文件?)更改该行为。
// Actions actions = new Actions(driver); // actions.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform(); ((JavascriptExecutor)driver).executeScript("window.open('about:blank', '_blank');"); Set tab_handles = driver.getWindowHandles(); int number_of_tabs = tab_handles.size(); int new_tab_index = number_of_tabs-1; driver.switchTo().window(tab_handles.toArray()[new_tab_index].toString());
getWindowHandles()方法现在返回一个Set。 到目前为止,这只在Chrome中进行了测试,因为Firefox 47目前在使用firefoxdriver时遇到了一些严重的问题……并且使用geckodriver时,Actions根本无法正常工作。 [更新6/11/2016]:Firefox通过geckodriver现在返回一个Set]
你也可以做这样的事情..把它投射到ArrayList:
// set tab_index to the number of window/tab you want. 0 is the first tab ArrayList tabs_windows = new ArrayList (driver.getWindowHandles()); driver.switchTo().window(tabs_windows.get(tab_index));
更新:为了解决geckodriver错误,我已经切换到使用element.sendkeys …这样的东西似乎在Marionette和Chrome中工作..(Update2):由于Selenium 3.0.1中的更改而更新为javascript:
// driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "t")); ((JavascriptExecutor)driver).executeScript("window.open('about:blank', '_blank');"); Set tab_handles = driver.getWindowHandles(); int number_of_tabs = tab_handles.size(); int new_tab_index = number_of_tabs-1; driver.switchTo().window(tab_handles.toArray()[new_tab_index].toString());
更新(2016年11月16日):使用Ctrl-W关闭的旧方法似乎也被破坏了…使用这个:
((JavascriptExecutor)driver).executeScript("close();");
我们手动切换到下一个标签的方法是按 – CTRL + Page Down
我们可以使用Selenium一样 –
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);
窗口句柄与索引号不是很安全,因为它们可能非常无序。 我建议你找一个清单并做一个循环,寻找预期的清单。
public void TabHandles() { driver.get("https://www.google.co.in/?gws_rd=ssl"); String currentWindowHandle = driver.getWindowHandle(); driver.findElement(By.linkText("Gmail")).sendKeys(Keys.CONTROL, Keys.RETURN); //Get the list of all window handles ArrayList windowHandles = new ArrayList (driver.getWindowHandles()); for (String window:windowHandles){ //if it contains the current window we want to eliminate that from switchTo(); if (window != currentWindowHandle){ //Now switchTo new Tab. driver.switchTo().window(window); //Do whatever you want to do here. //Close the newly opened tab driver.close(); } } }
您有一个可能的正确解决方案(Sol2),但问题是您无法切换到新选项卡,直到它不会被完全加载。
所以,解决方案:1)BAD ONE:放入等待计时器,只是睡觉(2000)一段时间,然后
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
2)好一个!
使用原生selenium的东西。 首先获取打开的所有可用选项卡:
driver.getWindowHandle();
然后转到另一个标签:
driver.switchTo().window(myWindowHandle );
您可以提供选项卡名称参数并尝试以下操作:
public boolean switchToTab(String tabName){ log.debug("Switch to {} tab",tabName); ArrayList tab = new ArrayList<>(driver.getWindowHandles()); ArrayList tabList = new ArrayList<>(); for (int i =0;i
我建议初始化第二个驱动程序来为该选项卡执行工作,或者在第一个驱动程序中打开第二个选项卡,并让该选项卡拥有自己的逻辑集来完成所需的操作。
下面的代码应该让您很好地了解如何使用Selenium 2.53.1和Chrome 51.0操作不同的驱动程序/浏览器/窗口以及每个驱动程序中的多个选项卡。
// INITIALIZE TWO DRIVERS (THESE REPRESENT SEPARATE CHROME WINDOWS) driver1 = new ChromeDriver(); driver2 = new ChromeDriver(); // LOOP TO OPEN AS MANY TABS AS YOU WISH for(int i = 0; i < TAB_NUMBER; i++) { driver1.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB Thread.sleep(100); // STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS ArrayList tabs1 = new ArrayList (driver1.getWindowHandles()); // REPEAT FOR THE SECOND DRIVER (SECOND CHROME BROWSER WINDOW) // LOOP TO OPEN AS MANY TABS AS YOU WISH for(int i = 0; i < TAB_NUMBER; i++) { driver2.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB Thread.sleep(100); // STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS ArrayList tabs2 = new ArrayList (driver1.getWindowHandles()); // NOW PERFORM DESIRED TASKS WITH FIRST BROWSER IN ANY TAB for(int ii = 0; ii <= TAB_NUMBER; ii++) { driver1.switchTo().window(tabs1.get(ii)); // LOGIC FOR THAT DRIVER'S CURRENT TAB } // PERFORM DESIRED TASKS WITH SECOND BROWSER IN ANY TAB for(int ii = 0; ii <= TAB_NUMBER; ii++) { drvier2.switchTo().window(tabs2.get(ii)); // LOGIC FOR THAT DRIVER'S CURRENT TAB }
我希望有所帮助
有一个简单而简短的方法:
import java.util.ArrayList; ArrayList tabs2 = new ArrayList (driver.getWindowHandles()); driver.switchTo().window(tabs2.get(1)); //Tab number //Can change it for next tab like that or previous: driver.switchTo().window(tabs2.get(1)); driver.close(); driver.switchTo().window(tabs2.get(0));
就是这样,希望它有所帮助。
下面是使用Robot Class的Java实现,我已经多次(7)次切换标签。
我希望它会有所帮助。
import:
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
主要方法
public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/Path/To/chromedriver/" + "chromedriver.exe"); WebDriver driver = new ChromeDriver(); // go to URL1 driver.navigate().to("http://www.facebook.com"); try { // Open New Tab by simulating Ctrl+t Robot r = new Robot(); r.keyPress(KeyEvent.VK_CONTROL); r.keyPress(KeyEvent.VK_T); r.keyRelease(KeyEvent.VK_CONTROL); r.keyRelease(KeyEvent.VK_T); Thread.sleep(1000); // Create Array List to keep Tab information ArrayList tabs2 = new ArrayList (driver.getWindowHandles()); // Navigate to New Tab driver.switchTo().window(tabs2.get(1)); // go to URL2 driver.navigate().to("http://www.google.com"); // Navigate to Tab 0 driver.switchTo().window(tabs2.get(0)); Thread.sleep(2000); // Navigate to Tab 1 driver.switchTo().window(tabs2.get(1)); Thread.sleep(2000); // Navigate to Tab 0 driver.switchTo().window(tabs2.get(0)); Thread.sleep(2000); // Navigate to Tab 1 driver.switchTo().window(tabs2.get(1)); Thread.sleep(2000); // Navigate to Tab 1 driver.switchTo().window(tabs2.get(0)); driver.close(); Thread.sleep(2000); // Navigate to Tab 1 driver.switchTo().window(tabs2.get(1)); driver.close(); } catch (Exception e) { } }