【发布时间】:2020-10-29 00:02:53
【问题描述】:
诚然,我对 Selenium WebDriver 非常陌生,但在 IntelliJ 中运行基本测试时遇到了障碍。如果我在代码中放置断点并在调试模式下运行它并逐步完成这些步骤,我就能够运行并通过我的测试。但是,当我定期单击运行时,它在第二步失败,说明如下:org.openqa.selenium.WebDriverException:元素在点(596.5、1128.63330078125)不可点击。其他元素会收到点击:
我一直在试图弄清楚为什么它会在 Debug 中按预期通过和运行,但在我定期运行时却没有。这是我的测试:
公共类 BF_Test {
private WebDriver driver;
@Test
public void checkURL()
{
WebDriver driver = BrowserFactory.getDriver("firefox");
driver.get("http://www.vizio.com");
WebElement homePagePopup = driver.findElement(By.xpath("//div[@id='modal']/div/a"));
homePagePopup.click();
//NEED TO FIGURE OUT WAIT TIMER OF SOME SORT
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("html body.cms-index-index.cms-index div.page-wrapper div.main.col1-layout div.std section#heroslider.kslider.hero-home.section-wrap.section-wrap-primary div.navi-wrap ul.navi.navi-bind.navi-count-3 li.item-0")));
WebElement naviButtonsCarousel = driver.findElement(By.cssSelector("html body.cms-index-index.cms-index div.page-wrapper div.main.col1-layout div.std section#heroslider.kslider.hero-home.section-wrap.section-wrap-primary div.navi-wrap ul.navi.navi-bind.navi-count-3 li.item-0"));
naviButtonsCarousel.click();
WebElement homePageTVPButton = driver.findElement(By.cssSelector("#tvp-marquee-inner > div.container > div.content > a.vz-btn"));
homePageTVPButton.click();
WebElement resultsAreInValidation = driver.findElement(By.cssSelector(".thanks.fade-in"));
System.out.println(resultsAreInValidation.getText());
WebElement robinsonBtn = driver.findElement(By.cssSelector("#robinsonHomeVote > span"));
robinsonBtn.click();
WebElement robinsonPlayerText = driver.findElement(By.cssSelector("#modal-container > div > div > div.player > div.playerInfo > section > h1"));
Assert.assertEquals("Verify that Allen Robinson text is on the page","Allen Robinson",robinsonPlayerText.getText());
WebElement btnClose = driver.findElement(By.cssSelector("button.close"));
btnClose.click();
driver.quit();
}
}
如您所见,我正在尝试做的事情并没有什么疯狂可报告的。只需打开一个网站,单击几个链接并断言一些文本等等。
对我可以做些什么来让它为我工作有什么想法吗?我一直在搞乱 WebDriverWait 看看是否是 Selenium 太快了......但似乎没有任何帮助。
谢谢!
【问题讨论】:
-
只需为我尝试一件事,而不是使用等待,您可以尝试在给出问题的代码块之前和之后使用 thread.sleep
-
@MrunalGosar 显式等待某个元素及其特定状态是 selenium-webdriver 中的最佳实践。睡眠从来都不是一个好主意,因为您的睡眠超时可能会更少或更多,因此会使您的测试不一致且不确定。使用 WebDriver 等待是同步问题的最佳解决方案。有关更多信息,请查看此答案:stackoverflow.com/a/30987106/5595994
-
@hashmatullah 同意显式等待是 Idea 的解决方案,但对于故障排除和调试问题,使用休眠没有害处,但在生产中是的,我们应该依赖显式等待
标签: java selenium selenium-webdriver intellij-idea