【问题标题】:Dynamic waiting for element to become clickable not working动态等待元素变为可点击不起作用
【发布时间】:2021-08-21 07:37:40
【问题描述】:

我写了一个函数如下

public static WebElement waitForElementToBeClickable(WebDriver driver, WebElement webElement, int seconds) {
    
    WebDriverWait wait = new WebDriverWait(driver, seconds);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(webElement));
    element.click();
    return element;
    
}

现在我正在导入这个函数并如下使用它

private String loginButton = "btnActive";
private String home = "//a[@id='pt1:_UIShome']";
WebElement login = driver.findElement(By.id(loginButton));
libraryUtils.waitForElements.waitForElementToBeClickable(driver, login, 20).click();
WebElement homeButton = driver.findElement(By.xpath(home));
libraryUtils.waitForElements.waitForElementToBeClickable(driver, homeButton, 20).click();
    

点击了登录按钮,但是在转到下一页后,我收到以下错误 -

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attach to the page document

Xpath 是正确的,我已经检查过了。当我使用element.click() 时,它也可以工作,但是这需要我手动输入睡眠时间,这就是我正在做的事情。我希望这是动态的。感谢任何帮助或建议。

【问题讨论】:

  • 位于//a[@id='pt1:_UIShome'] XPath 的元素是否出现在第一页,loginButton 出现的位置?
  • 你能分享一个网站链接吗?
  • 嘿抱歉,我不允许共享链接,但我登录后页面上没有带有 //a[@id='pt1:_UIShome'] 的元素
  • 好的,这个元素是否在视图中,或者您必须滚动到它才能将它带到可见屏幕?最初上面是否有某种弹出窗口、微调器等?
  • 它在视图中,不需要scrool或任何东西

标签: java selenium selenium-webdriver automation rpa


【解决方案1】:

有时 JS 还没有完成运行,如果你尝试与 web 元素web 元素 交互,你会看到staleElemenent exception

但是以下解决方案可能对您有用:

new WebDriverWait(driver, 10).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='pt1:_UIShome']")));
driver.findElement(By.xpath("//a[@id='pt1:_UIShome']")).click();

【讨论】:

    【解决方案2】:

    可能这不太好,但我认为除了在那里增加一些睡眠之外没有其他稳定的解决方案。 所以,请试试这个:

    private String loginButton = "btnActive";
    private String home = "//a[@id='pt1:_UIShome']";
    WebElement login = driver.findElement(By.id(loginButton));
    libraryUtils.waitForElements.waitForElementToBeClickable(driver, login, 20).click();
    driver.findElement(By.xpath(home));
    libraryUtils.waitForElements.waitForElementToBeClickable(driver, homeButton, 20);
    try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
    }
    driver.findElement(By.xpath(home)).click();
    

    【讨论】:

    • 我已经在手动暂停它时添加了睡眠,我想动态地执行它,以便它可以在多台机器上工作,而不管互联网速度的延迟。问题是我必须在更多的地方增加睡眠,而不仅仅是这个。不过谢谢。
    • 我明白了。如果存在动态 DOM,我认为没有简单而稳定的解决方案。这是一个已知问题。
    • 如果您找到一些清晰稳定的解决方案,请告诉我。
    • 是的,当然,如果你也这样做,请告诉我
    【解决方案3】:

    这里的问题是你已经在waitForElementToBeClickable函数中做element.click(),并且在调用这个函数的时候再次调用click。

    libraryUtils.waitForElements.waitForElementToBeClickable(driver, login, 20).click();

    由于您在等待元素可点击时已经点击过,因此您不应该再次点击它。我希望你能得到StaleElementException的答案

    【讨论】:

    • WebElement login = driver.findElement(By.id(loginButton)); libraryUtils.waitForElements.waitForElementToBeClickable(驱动程序,登录,20); WebElement homeButton = driver.findElement(By.xpath(home)); libraryUtils.waitForElements.waitForElementToBeClickable(driver, homeButton, 20); WebElement payablesButton = driver.findElement(By.xpath(payables)); libraryUtils.waitForElements.waitForElementToBeClickable(驱动程序,payablesButton,20);
    • 我更改了代码并执行了它,如上所示,它点击了 homeButton 元素,但没有点击payablesButton 元素。我收到以下错误 - org.openqa.selenium.ElementClickInterceptedException:元素点击被拦截:元素 ... 不可点击
    • 出现此错误的原因是某些其他元素的子元素或父元素与您尝试单击的元素重叠。几种修复方法是:element = driver.find_element_by_css('div[class*="class_name"]') driver.execute_script("arguments[0].click();", element)element = driver.find_element_by_css('div[class*="class_name"]') webdriver.ActionChains(driver).move_to_element(element ).click(element ).perform()
    猜你喜欢
    • 1970-01-01
    • 2022-11-28
    • 2020-02-23
    • 1970-01-01
    • 2015-10-11
    • 2016-12-29
    • 2018-02-19
    • 2020-03-29
    • 1970-01-01
    相关资源
    最近更新 更多