使用Selenium切换活动标签页
我已经开发了这段代码,用于将所有搜索结果打开到新标签中:
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);results.get(i).sendKeys(selectLinkOpeninNewTab);
for (int i = 0; i < results.size(); i++)
{
ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.close();
driver.switchTo().window(tabs2.get(0));
}
我想要的是当我打开所有带有搜索结果的选项卡时,我想在所有选项卡之间切换并延迟2秒。
我该如何实施?
回答:
试试这个代码,
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[@class='r']/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();
}
}
}
以上是 使用Selenium切换活动标签页 的全部内容, 来源链接: utcz.com/qa/397520.html