【问题标题】:Ask selenium to wait - WebDriverWait issue要求 selenium 等待 - WebDriverWait 问题
【发布时间】:2019-09-12 14:05:28
【问题描述】:

我遇到了无法解决的硒问题。 我收到以下错误”

no such element: Unable to locate element:{"method":"id","selector":"menu-supply-approval-queue"} 

我知道问题在于等待。 于是我做了下一个方法:

public static WebElement getWebElementByIdWithWait(String id)
    {
        WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 300000000);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-supply-approval-queue")));

        return WebDriverMgr.waitForElementToBeClickable(By.id(id));
    }

但是 selenium 没有等待,并且再次出现此错误:

Thu Sep 12 16:56:45 IDT 2019:ERROR: no such element: Unable to locate element: {"method":"id","selector":"menu-supply-approval-queue"}
  (Session info: chrome=76.0.3809.132)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '', ip: '', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver

有人可以建议如何使硒等待元素显示/可点击。 它没有等待一秒钟

更奇怪的是,selenium 居然返回了元素,然后点击了它,所以如果他找不到它是如何返回的呢?它只是弄乱了日志

这是有效的代码,但它使用睡眠

 public static WebElement getWebElementByIdWithWait(String id)
    {
        Logger.sleep(60000);
      //  WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 8);

     //   wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-supply-approval-queue")));

        return WebDriverMgr.waitForElementToBeClickable(By.id(id));
    }

问候

【问题讨论】:

  • 看起来您使用的是非常旧的 chromedriver。您可以将其升级到 76。
  • 我认为它在 waitForElementToBeClickable 行上失败了?
  • 显示有关错误的更多详细信息。也许是堆栈跟踪?
  • 奇怪的是它没有失败,它只是把它放在日志中,
  • 可以分享网址吗?

标签: java selenium google-chrome selenium-webdriver selenium-chromedriver


【解决方案1】:

此错误消息...

Thu Sep 12 16:56:45 IDT 2019:ERROR: no such element: Unable to locate element: {"method":"id","selector":"menu-supply-approval-queue"}
  (Session info: chrome=76.0.3809.132)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '', ip: '', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver

...暗示 ChromeDriver 无法启动/生成新的 WebBrowserChrome 浏览器 会话。

您的主要问题是您使用的二进制文件版本之间的不兼容性,如下所示:

  • 您正在使用 chromedriver=2.36
  • chromedriver=2.36 的发行说明明确提及以下内容:

支持 Chrome v63-65

  • 您正在使用 chrome=76.0
  • ChromeDriver v76.0 的发行说明明确提及以下内容:

支持Chrome 76版

  • 您的JDK 版本1.8.0_65,相当古老

所以 JDK v8u65ChromeDriver v2.36Chrome 浏览器 v76.0

之间存在明显的不匹配

解决方案

确保:

【讨论】:

    【解决方案2】:

    有一个预期的条件 elementToBeClickable

    WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 60);
            wait.until(ExpectedConditions.elementToBeClickable(By.id("menu-supply-approval-queue")));
    

    我不确定 waitForElementToBeClickable 调用在哪里/做什么以及为什么它可能会失败。

    【讨论】:

    • 对不起,但仍然没有,我放了 8 并且仍然硒不等待。非常奇怪的是 selenium 返回这个元素,并在其他方法中点击它。问题只是在日志中
    【解决方案3】:

    您发布的方法有一个 id 字符串参数,但您在第二行硬编码了等待特定 ID 的代码

    public static WebElement getWebElementByIdWithWait(String id)
    {
        WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 300000000);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-supply-approval-queue")));
                                                                 ^ this is hardcoded
    
        return WebDriverMgr.waitForElementToBeClickable(By.id(id));
    }
    

    你试过去掉那条线吗?

    public static WebElement getWebElementByIdWithWait(String id)
    {
        WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 300000000);
        return wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id));
    }
    

    如果是我,我会使用如下方法。它不是特定于 ID(您传入 By,因此它可以与 ID、CSS 选择器、XPath 等一起使用),它会为您处理点击,因此等待特定于可点击。

    public static void click(By locator)
    {
        new WebDriverWait(WebDriverMgr.getDriver(), 15).until(ExpectedConditions.elementToBeClickable(locator)).click();
    }
    

    你会这样称呼它

    click(By.id("menu-supply-approval-queue"));
    

    【讨论】:

      【解决方案4】:

      也许你可以试试“Thread.Sleep(1000);”喜欢

      【讨论】:

      • 你好。除了提供解决方案外,请解释为什么答案可能会解决 OP 的问题 :)
      【解决方案5】:

      经过调查,我发现了这个问题,隐式等待太小了。 解决方案:

      【讨论】:

      • 把隐式等待 25 和显式等待 35
      • the docs, WARNING: Do not mix implicit and explicit waits! Doing so can cause unpredictable wait times.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 2021-04-01
      • 2022-01-23
      相关资源
      最近更新 更多