为了账号安全,请及时绑定邮箱和手机立即绑定

让 selenium 等待 - WebDriverWait 问题

让 selenium 等待 - WebDriverWait 问题

一只斗牛犬 2023-07-28 15:28:48
我面临着无法解决的硒问题。我收到以下错误”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 millisecondsFor documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.htmlBuild 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));    }问候
查看完整描述

4 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

这个错误信息...


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无法启动/生成新的WebBrowser(即Chrome 浏览器会话)。

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

  • 您正在使用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之间存在明显的不匹配


解决方案

确保这件事:

  • JDK已升级到当前级别JDK 8u222。

  • ChromeDriver已更新至当前ChromeDriver v77.0级别。

  • Chrome已更新至当前Chrome 版本 77.0级别。(根据ChromeDriver v77.0 发行说明)

  • @Test非 root用户身份执行。


查看完整回答
反对 回复 2023-07-28
?
米脂

TA贡献1836条经验 获得超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(您传入 a 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"));


查看完整回答
反对 回复 2023-07-28
?
冉冉说

TA贡献1877条经验 获得超1个赞

存在预期条件 elementToBeClickable

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

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


查看完整回答
反对 回复 2023-07-28
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

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



查看完整回答
反对 回复 2023-07-28
  • 4 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信