【问题标题】:Selenium Webdriver wait Expected condition failed: waiting for visibility of element located by By.idSelenium Webdriver 等待预期条件失败:等待 By.id 定位的元素的可见性
【发布时间】:2018-09-18 16:18:50
【问题描述】:

我正在尝试让 selenium web 驱动程序等待,但我总是遇到异常“

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

我将秒数增加到 100,然后也遇到了同样的问题,我的 id 是正确的。

    WebDriver d = new ChromeDriver();
    d.get("http://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("http://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 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>

【问题讨论】:

  • 页面的html是什么?这是您需要检查的真实 div 吗?
  • 您收到异常是因为您没有正确处理异常并且元素在 20 秒后未定位。
  • 它真正的 div..我检查了 dat
  • 是的元素在 20 秒后没有找到,我也尝试了 100 秒,den 也一样..
  • 检查它是否在框架内?

标签: selenium selenium-webdriver


【解决方案1】:

根据WebDriverWait 类的Java Docs,如果你想改变轮询间隔,你需要在构造函数中改变它因为构造函数如下:

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.

在您尝试在需要调用ExpectedConditions 方法elementToBeClickable 的元素上调用sendKeys() 时向前推进。

所以你的代码将是:

WebDriver d = new ChromeDriver();
d.get("http://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("http://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());
} 

【讨论】:

  • 感谢您的回复,我尝试了您的代码。但第一次工作,第二次不工作“org.openqa.selenium.TimeoutException:预期条件失败:等待元素可点击: By.xpath: //input[@class='form-control k-input' and @id='mobileNo'](尝试20秒,间隔100毫秒)"
  • 我没有使用任何框架,只有 div..is 可能是原因?当我尝试元素可见测试时,有时元素不可见,我认为元素在驱动程序运行后出现
  • 还是一样的 erro..org.openqa.selenium.TimeoutException: 预期条件失败: 等待元素可点击: By.xpath: //input[@class='form-control k-input ' 和 @id='mobileNo'](尝试 20 秒,间隔 100 毫秒)
  • 我没有使用框架。只有完整的 div。我们没有使用框架
  • ELEMENT
猜你喜欢
  • 2019-04-20
  • 2017-10-17
  • 2019-08-03
  • 2019-11-25
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
  • 2016-07-28
相关资源
最近更新 更多