让 selenium 等待 - WebDriverWait 问题

我面临着无法解决的硒问题。我收到以下错误”


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));

    }

问候


一只斗牛犬
浏览 117回答 4
4回答

守着星空守着你

这个错误信息...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..意味着ChromeDriver无法启动/生成新的WebBrowser(即Chrome 浏览器会话)。您的主要问题是您使用的二进制文件版本之间不兼容,如下所示:您正在使用chromedriver=2.36chromedriver=2.36的发行说明明确提到了以下内容:支持Chrome v63-65您正在使用chrome=76.0ChromeDriver v76.0的发行说明明确提到了以下内容:支持Chrome 版本 76您的JDK 版本是1.8.0_65,这是相当古老的。因此JDK v8u65、ChromeDriver v2.36和Chrome 浏览器 v76.0之间存在明显的不匹配解决方案确保这件事:JDK已升级到当前级别JDK 8u222。ChromeDriver已更新至当前ChromeDriver v77.0级别。Chrome已更新至当前Chrome 版本 77.0级别。(根据ChromeDriver v77.0 发行说明)@Test以非 root用户身份执行。

米脂

您发布的方法有一个 id 的字符串参数,但您在第二行中硬编码等待特定 IDpublic 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"));

冉冉说

存在预期条件 elementToBeClickableWebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 60);         wait.until(ExpectedConditions.elementToBeClickable(By.id("menu-supply-approval-queue")));我不确定 waitForElementToBeClickable 调用在哪里/做什么以及为什么它可能会失败。

慕哥6287543

也许你可以尝试“Thread.Sleep(1000);” 喜欢
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java