【问题标题】:Selenium Java unknown time Loading of browserSelenium Java 未知时间加载浏览器
【发布时间】:2018-10-24 13:12:45
【问题描述】:

我是 selenium Java 的新手,谁能帮忙给我一个简单的例子,如何创建一个 wait.until java 命令?如有错误请更正我的代码谢谢

import org.openqa.selenium.support.ui.WebDriverWait;
//lines of code
WebDriverWait waitVar = new WebDriverWait (driver, 1000);
waitVar.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"btnDraftReports\"]"))).click();

还是我应该用这个?

waitVar.until(ExpectedConditions.elementToBeClickable(By.id("btnDraftReports"))).click();

谢谢你和更多的力量

【问题讨论】:

  • 用相关的 HTML 更新问题
  • @ian castillio 你可以同时使用。 elementToBeClicable() 我用于角度元素。 visibilityOfElementLocated() 我用于“通用”元素,如按钮等。

标签: java selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

对于 Selenium,这两个表达式在语法上都是正确且有效的定位器策略,其中:

  • By.xpath("//*[@id=\"btnDraftReports\"]") :此表达式使用 xpath 引擎。
  • By.id("btnDraftReports") :此表达式使用 id 属性。

但是,一旦您在调用 click() 时等待 ExpectedConditions 返回元素,因此您应该使用 elementToBeClickable() 方法而不是 visibilityOfElementLocated() 方法,如下所示:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"btnDraftReports\"]"))).click();
//or
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("btnDraftReports"))).click();

琐事

现在,有争议的问题是使用哪个定位器idxpath

由于 Selenium 在实践中 id 属性仍然被认为是最有效的定位器策略

您将在Official locator strategies for the webdriver讨论中获得更多见解

【讨论】:

    【解决方案2】:

    visibilityOfElementLocatedelementToBeClickable 行为不同。

    何时使用什么:所有预期条件取决于场景。

    visibilityOfElementLocated :当您想等到网页元素的可见性显示在网页上时,您可能想要使用此预期条件。它更多的是关于网络元素的可见性。

    示例:

    1. 登录到任何网站后,您应该让脚本知道登录已通过使用 visibilityOfElementLocated 条件成功完成。您可能会考虑您的个人资料图片或登录后出现的任何链接。

    elementToBeClickable :当您想等到 Web 元素变为可点击时。它更多的是关于网络元素的点击能力。

    示例:

    1. 您在创建帐户时一定在许多网站中看到了协议复选框。这种情况是在您选中阅读协议复选框之前,您无法单击注册按钮。在这里,您将不得不使用 elementToBeClickable 条件。

    关于我认为您编写正确的代码。

    希望这将帮助您了解上述条件之间的基本区别。

    【讨论】:

    • 线程“主”org.openqa.selenium.WebDriverException 中的异常:未知错误:元素 ... 不可点击点 (735, 120)。其他元素会收到点击:
      。 ..
      仍然收到此错误消息:(
    • waitVar.until(ExpectedConditions.elementToBeClickable(By.id('btnDraftReports'))); driver.findElement(By.id(“btnDraftReport”)).click();只要确保元素是可见的,如果有任何向下或向上滚动,你就表现得很好。
    • 线程“主”org.openqa.selenium.WebDriverException 中的异常:未知错误:元素 ... 不可点击点 (735, 124)。其他元素会收到点击:
      。 ..
      仍然遇到错误
    • 当您手动执行此操作时,您是否必须向下滚动才能与元素交互或您必须执行的任何类型的交互操作?
    【解决方案3】:

    试试下面的代码

    /* This method is used for wait until element found */
    public void expliciteWait(WebElement element, int timeToWaitInSec, WebDriver driver) {
        WebDriverWait wait = new WebDriverWait(driver, timeToWaitInSec);
        wait.until(ExpectedConditions.visibilityOf(element));
    

    【讨论】:

      【解决方案4】:

      WebElement XYZ= driver.findElement(By.xpath("xpath")); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.elementToBeClickable(XYZ)); XYZ.click; 为了获得更好的答案,您需要显示 HTML

      【讨论】:

        猜你喜欢
        • 2017-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 2011-09-14
        • 2019-02-09
        • 1970-01-01
        相关资源
        最近更新 更多