【问题标题】:How to click with Selenium Webdriver and Java如何点击 Selenium Webdriver 和 Java
【发布时间】:2020-10-31 22:32:14
【问题描述】:

我正在执行自动化任务。

为了完成我的任务,我想在同一个弹出窗口中执行 3 次自动点击。最后一个失败了很多次。有时它无法点击或在按钮外点击。

我也试过thread.sleep,问题依旧。

我的代码:

WebElement boton = driver.findElement(By.xpath(PATH_BOTON));
WebDriverWait wait3 = new WebDriverWait(driver, 2);
wait3.until(ExpectedConditions.elementToBeClickable(boton)).click();

CSS 选择器或 Javascript 执行器是更好的选择?

【问题讨论】:

    标签: java selenium selenium-webdriver webdriverwait


    【解决方案1】:

    在按钮上使用 Selenium 进行测试时,我遇到的大多数问题都来自按钮对象在 html 页面上的开始位置。跨度元素本身有时​​会在按钮之前开始,因此您最终会单击空白跨度空间而不是按钮,如果是这种情况,您可能需要使用 DRIVER.MoveByOffset(xInt, yInt).Perform() 方法来移动您的指针到位。 至于您的选择器,最好尽可能使用 By.id("uniqueID"),因为只能给出一个唯一的 id,因此您知道那是您为测试而抓取的对象。

    【讨论】:

    • selenium 指针偏移量通常从您选择的元素的左上角开始。
    【解决方案2】:

    要在元素上调用click() 三次,您需要为elementToBeClickable() 诱导WebDriverWait,您可以使用以下locator strategies

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("PATH_BOTON"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("PATH_BOTON"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("PATH_BOTON"))).click();
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2016-06-24
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多