gpt4 book ai didi

java - 在 Firefox 浏览器中打开新选项卡并尝试向下滚动页面并单击链接失败

转载 作者:行者123 更新时间:2023-12-02 01:08:05 24 4
gpt4 key购买 nike

我有这个功能,我试图向下滚动页面并单击链接。我已将代码放入 for 循环中,因为我想打开多个选项卡。我试图单击的链接不在窗口 View 中,它们位于所有网页通用的页脚中。我的方法应该向下滚动,直到要单击的链接可见,然后按住 Control 键并单击并打开一个新选项卡。该方法在 Chrome 和 Internet Explorer 浏览器中运行良好,但在 Firefox 中失败,提示要单击的链接不存在。我认为尽管我放置了向下滚动的代码,但它并没有向下滚动。请帮忙。

public static void checkHrefsWithBrowserUrls(List<WebElement> links) 
{
String parentTab = null;
String clickOnLink = Keys.chord(Keys.CONTROL, Keys.ENTER);

log.debug("Checking that the links open the correct url");
for (WebElement link : links) {
((JavascriptExecutor)driver)
.executeScript("arguments[0].scrollIntoView(true);", link);
String href = link.getAttribute("href");
link.sendKeys(clickOnLink);
WaitUtilities.sleep(1L);
Iterator<String> handleIterator = driver.getWindowHandles().iterator();

parentTab = handleIterator.next();

if(handleIterator.hasNext()) {
driver.switchTo().window(handleIterator.next());
WaitUtilities.waitForUrlToBe(url());
if(!href.equals(url())) {
log.error("Link(s) opening wrong URL(s): " + url());
}
driver.close();
driver.switchTo().window(parentTab);
}
}
driver.switchTo().window(parentTab);
}

最佳答案

这是处理状态元素问题的伪代码。

public static void checkHrefsWithBrowserUrls(String xpath) 
{
String parentTab = null;
String clickOnLink = Keys.chord(Keys.CONTROL, Keys.ENTER);

log.debug("Checking that the links open the correct url");
int linksCount = driver.findElements(By.xpath(xpath)).size();
for (int linkCounter=1; linkCounter=linksCount, linkCounter++) {
link = driver.findElements(By.xpath(xpath)).get(linkCounter)
((JavascriptExecutor)driver)
.executeScript("arguments[0].scrollIntoView(true);", link);
String href = link.getAttribute("href");
link.sendKeys(clickOnLink);
WaitUtilities.sleep(1L);
Iterator<String> handleIterator = driver.getWindowHandles().iterator();

parentTab = handleIterator.next();

if(handleIterator.hasNext()) {
driver.switchTo().window(handleIterator.next());
WaitUtilities.waitForUrlToBe(url());
if(!href.equals(url())) {
log.error("Link(s) opening wrong URL(s): " + url());
}
driver.close();
driver.switchTo().window(parentTab);
}
}
driver.switchTo().window(parentTab);
}

关于java - 在 Firefox 浏览器中打开新选项卡并尝试向下滚动页面并单击链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712305/

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