猿问

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

我有这个功能,我试图向下滚动页面并单击链接。我已将代码放入 for 循环中,因为我想打开多个选项卡。我试图单击的链接不在窗口视图中,它们位于所有网页通用的页脚中。我的方法应该向下滚动,直到要单击的链接可见,然后按住 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);

}


DIEA
浏览 128回答 1
1回答

牛魔王的故事

这是处理状态元素问题的伪代码。public static void checkHrefsWithBrowserUrls(String xpath)&nbsp;{&nbsp; &nbsp; &nbsp;String parentTab = null;&nbsp; &nbsp; &nbsp;String clickOnLink = Keys.chord(Keys.CONTROL, Keys.ENTER);&nbsp; &nbsp; &nbsp;log.debug("Checking that the links open the correct url");&nbsp; &nbsp; &nbsp;int linksCount = driver.findElements(By.xpath(xpath)).size();&nbsp; &nbsp; &nbsp;for (int linkCounter=1; linkCounter=linksCount, linkCounter++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;link = driver.findElements(By.xpath(xpath)).get(linkCounter)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;((JavascriptExecutor)driver)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .executeScript("arguments[0].scrollIntoView(true);", link);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String href = link.getAttribute("href");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;link.sendKeys(clickOnLink);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WaitUtilities.sleep(1L);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Iterator<String> handleIterator = driver.getWindowHandles().iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parentTab = handleIterator.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(handleIterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;driver.switchTo().window(handleIterator.next());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WaitUtilities.waitForUrlToBe(url());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(!href.equals(url())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.error("Link(s) opening wrong URL(s): " + url());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; driver.close();&nbsp; &nbsp; &nbsp;driver.switchTo().window(parentTab);&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;driver.switchTo().window(parentTab);}
随时随地看视频慕课网APP

相关分类

Java
我要回答