gpt4 book ai didi

java - 在新标签页中打开 Google 链接

转载 作者:行者123 更新时间:2023-11-30 02:53:01 25 4
gpt4 key购买 nike

我想在新标签页中打开 Google 搜索结果。我尝试了这段代码:

WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000);
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
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 (int i = 0; i < results.size(); i++)
{
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.xpath("//h3[@class='r']/a")).sendKeys(selectLinkOpeninNewTab);

System.out.println("Google result " + results.get(i).getText());
}

到目前为止,代码会打开空选项卡并打印搜索结果的名称。如何打开搜索结果中的链接?

最佳答案

只需更改循环内的以下行:-

driver.findElement(By.xpath("//h3[@class='r']/a")).sendKeys(selectLinkOpeninNew‌​Tab);

results.get(i).sendKeys(selectLinkOpeninNewTab);

它正在工作......:)

关于java - 在新标签页中打开 Google 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38073545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com