【问题标题】:Selenium WebDriver passes in Debug but not when regularly runSelenium WebDriver 在 Debug 中传递,但在定期运行时不传递
【发布时间】: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


【解决方案1】:

WebDriverException: Element is not clickable at point 表示您要点击的元素不可见。您可以通过滚动到点击之前的元素来解决它。

WebElement element = driver.findElement(By.cssSelector("selector"));

Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(element)).click();

【讨论】:

    【解决方案2】:

    为我工作,在 C# 中使用 Selenium,3.0 的以下说明:

    Thread.Sleep(TimeSpan.FromSeconds(15));
    

    然后我可以创建 webElement 并发送事件点击。

    【讨论】:

    • Thead.Sleep 不是好的做法,请使用更好的替代方法,例如在 WaitHandle 上调用 WaitOne 或在 C# 中使用取消标记的任务。检查这个答案:stackoverflow.com/a/5424747/5595994
    【解决方案3】:

    像这样创建一个扩展:

     public static class WebDriverExtensions
    {
        public static IWebElement FindElementUntil(this IWebDriver driver, By by, int waitSeconds = 15)
        {
            var wait = new WebDriverWait(driver, System.TimeSpan.FromSeconds(waitSeconds));
            wait.Until(driver => driver.FindElement(by));
            return driver.FindElement(by);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多