【问题标题】:Why isn't my click event not working in Selenium?为什么我的点击事件在 Selenium 中不起作用?
【发布时间】:2018-03-10 08:18:50
【问题描述】:

当 clickevent 被触发时,我希望它在同一个选项卡中重定向/打开新页面。新选项卡将是“/Waiting”,但是即使在触发单击事件后,它仍会保留在同一页面中。通过转到浏览器的本地主机手动进行时,它可以工作。此外,即使在 10 秒后,它也不会加载。

@Test
public void firstPlayerConnection() {
    try {
        driver.get(uiPath);
        WebElement startGame = driver.findElement(By.id("startGame"));
        startGame.click();
        WebElement gif = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.id("loading")));
        assertEquals("/Waiting", driver.getCurrentUrl());

    } finally {
        driver.quit();
    }
}

【问题讨论】:

  • startGame.click() 是否出现空指针异常?
  • 任何 Selenium 异常抛出,如 NoSuchElement、ElementNotVisible 等?
  • 是的。由于页面没有加载,它说 nosuchelement
  • 当您的问题所在的代码中出现异常时,请始终提供异常的堆栈跟踪,并明确发生此异常的代码行。这样可以更轻松地快速回答您的问题。
  • noSuchElement 异常不是因为页面没有加载,而是因为 startGame 按钮在你点击它的那一刻是不可点击的。

标签: java selenium testing web junit


【解决方案1】:

明确提供等待,如果仍然无法正常工作,请尝试使用 submit() 代替 click()。

【讨论】:

    【解决方案2】:

    如果我没记错的话,NoSuchElement 异常必须在 startGame.Click() 命令上。为避免此异常,您必须(显式)等待此元素可点击。

    WebElement startGame = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.elementToBeClickable(By.id("startGame")));
    

    【讨论】:

    • 这有帮助。这是因为没有隐式/显式等待
    • 在您的情况下,它必须是显式的,因为您的代码中已经有显式等待,混合隐式和显式等待并不是一个好主意,因为它会产生不可预测的等待时间。请参阅:seleniumhq.org/docs/04_webdriver_advanced.jsp
    猜你喜欢
    • 2020-08-14
    • 1970-01-01
    • 2021-12-28
    • 2020-12-27
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多