使用Selenium切换活动标签页

使用Selenium切换活动标签页,第1张

使用Selenium切换活动标签

试试这个代码,

import java.awt.AWTException;import java.awt.Robot;import java.awt.event.KeyEvent;import java.util.ArrayList;import java.util.List;import org.openqa.selenium.By;import org.openqa.selenium.Keys;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;public class TabSwitching {    public static void main(String[] args) throws InterruptedException, AWTException {        System.setProperty("webdriver.chrome.driver", "D:/WorkSpace/Driver/chromedriver.exe");        WebDriver driver = new ChromeDriver();        driver.get("http://www.google.com/xhtml");        Thread.sleep(5000);        WebElement searchBox = driver.findElement(By.name("q"));        searchBox.sendKeys("Software testing");        searchBox.submit();        System.out.println("Current Url: " + driver.getCurrentUrl());        WebDriverWait wait = new WebDriverWait(driver, 10);        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("h3.r a")));        List<WebElement> results = driver.findElements(By.xpath("//h3[@]/a"));        for (WebElement ele : results) { String selectlinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN); ele.sendKeys(selectlinkOpeninNewTab);        }        Thread.sleep(5000);        ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());        System.out.println(tabs2.size());        for (int i = tabs2.size()-1; i>=0; i--) { Thread.sleep(2000); driver.switchTo().window(tabs2.get(i)); Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_CONTROL); System.out.println(driver.getTitle() + "i: " + i); driver.close();        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5639365.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存