用Java使用Selenium WebDriver切换选项卡
/
猿问
用Java使用Selenium WebDriver切换选项卡
psdbComponent.clickDocumentLink(); ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles()); driver.switchTo().window(tabs2.get(1)); driver.close(); driver.switchTo().window(tabs2.get(0));
@Testpublic 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());}
案例1:
//Get the current window handleString windowHandle = driver.getWindowHandle(); //Get the list of window handlesArrayList tabs = new ArrayList (driver.getWindowHandles()); System.out.println(tabs.size()); //Use the list of window handles to switch between windowsdriver.switchTo().window(tabs.get(0)); //Switch back to original windowdriver.switchTo().window(mainWindowHandle);
案例2:
//Open a new tab using Ctrl + tdriver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); //Switch between tabs using Ctrl + \tdriver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
举报