Selenium Webdriver wait预期条件失败:等待By.id定位的元素的可见性

编程入门 行业动态 更新时间:2024-10-28 16:28:15
本文介绍了Selenium Webdriver wait预期条件失败:等待By.id定位的元素的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让Selenium Web驱动程序等待,但总是会出现异常"

I am trying to put selenium web driver wait, but always I am getting an exception "

org.openqa.selenium.TimeoutException:预期条件失败:等待By.id定位的元素的可见性:mobileNo(以100毫秒间隔尝试20秒)".

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.id: mobileNo (tried for 20 second(s) with 100 milliseconds interval)".

我将秒数增加到100,然后又遇到了同样的问题,我的ID是正确的.

I increased seconds to 100, then also getting the same problem, my id is correct.

WebDriver d = new ChromeDriver(); d.get("myurlOne"); WebElement username = d.findElement(By.id("username_id")); username.sendKeys("123"); WebElement password = d.findElement(By.id("password_id")); password.sendKeys("123"); d.findElement(By.id("loginButton")).click(); System.out.println("logged in successfully"); d.get("navigatedurl"); JavascriptExecutor js = (JavascriptExecutor)d; System.out.println("navigated to new page"); WebDriverWait wait__mob = new WebDriverWait(d, 20); try { System.out.println("Start"+new Date()); wait__mob .pollingEvery(100,TimeUnit.MILLISECONDS).until(ExpectedConditions.visibilityOfElementLocated(By.id("mobileNo"))); d.findElement(By.id("mobileNo")).sendKeys("99999999999); } catch (TimeoutException e) { // TODO: handle exception System.out.println(e.toString()); }

div代码:

<div class="form-group"> <label class="col-xs-5 control-label" for="mobileNo">Mobile No.</label> <div class="col-xs-6 leftpadding-none"> <input type="tel" class="form-control k-input" id="mobileNo" name="inputmobileNo" placeholder="" maxlength="10"> <!--required pattern="\d{10}" validationMessage="Mobile No. is Required"--> </div>

推荐答案

根据Java文档/java/org/openqa/selenium/support/ui/WebDriverWait.html"rel =" nofollow noreferrer> WebDriverWait 类,如果您想更改轮询间隔,则需要对其进行更改在 constructor 中作为构造函数如下:

As per the Java Docs of WebDriverWait Class if you want to change the Polling Interval you need to change it in the constructor as the constructor is as follows :

WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis) Wait will ignore instances of NotFoundException that are encountered (thrown) by default in the 'until' condition, and immediately propagate all others.

在尝试在元素上调用sendKeys()时继续前进,您需要调用ExpectedConditions方法 elementToBeClickable .

Moving forward as you are trying to invoke sendKeys() on the element you need to invoke the ExpectedConditions method elementToBeClickable.

所以您的代码将是:

WebDriver d = new ChromeDriver(); d.get("myurlOne"); WebElement username = d.findElement(By.id("username_id")); username.sendKeys("123"); WebElement password = d.findElement(By.id("password_id")); password.sendKeys("123"); d.findElement(By.id("loginButton")).click(); System.out.println("logged in successfully"); d.get("navigatedurl"); System.out.println("navigated to new page"); WebDriverWait wait__mob = new WebDriverWait(d, 20); try { System.out.println("Start"+new Date()); wait__mob.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='form-group']//label[contains(.,'Mobile No.')]//following::div[1]/input[@class='form-control k-input' and @id='mobileNo' and @type='tel']"))).sendKeys("9999999999); } catch (TimeoutException e) { System.out.println(e.toString()); }

更多推荐

Selenium Webdriver wait预期条件失败:等待By.id定位的元素的可见性

本文发布于:2023-11-08 10:42:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   条件   见性   Webdriver   Selenium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!