【问题标题】:Selenium Java-Object changes location dynamicallySelenium Java-Object 动态更改位置
【发布时间】:2020-01-10 02:33:47
【问题描述】:

有 3 类用户:授权用户、非授权用户和总用户。用户可以通过 UI 创建,默认情况下,用户将显示在未授权部分。三个页面部分中的每一个部分都有一个可供用户使用的切换按钮。当用户从非授权部分单击用户的切换按钮时,用户条目将进入授权页面。

自动化要求是点击用户的每个切换按钮,检查是否所有按钮都被点击,然后导航到页面用户,检查是否添加了所有用户。

问题在于,当任何用户单击切换按钮时,顶部/底部记录 HTML 位置会被前面的位置替换。 例如:如果第一个切换按钮的路径是 //[@id='xyz']/td1 并且用户单击此切换按钮,则下一个用户记录切换按钮的路径现在与上面相同,即/ /[@id='xyz']/td1

下面是拨动开关的html代码: <span class="bootstrap-switch-handle-off bootstrap-switch-default" style="width: 20px;"></span>

我尝试在每次切换按钮单击后添加 Thread.sleep() 以便有延迟,然后我的代码可以使用相同的路径单击下一个切换开关,但想知道是否有优化的处理方式这个场景。

有什么建议吗?

【问题讨论】:

  • 以文本格式分享html

标签: java selenium automation ui-automation


【解决方案1】:

尝试单击第一个切换并等待它在while循环中消失,任何切换存在条件:

WebDriverWait wait = new WebDriverWait(driver, 10);
while (driver.findElements(By.cssSelector(".bootstrap-switch-handle-off")).size() > 0) {
    WebElement toggle =
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".bootstrap-switch-handle-off")));
    toggle.click();
    //wait.until(ExpectedConditions.invisibilityOf(toggle));
    wait.until(d -> {
        try {
            return !toggle.isDisplayed();
        } catch (StaleElementReferenceException ignored) {
           return true;
        }
    });
}

【讨论】:

  • 点击wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".bootstrap-switch-handle-off")))行上的第一个切换后代码失败;
  • org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for element to no longer be visible: By.cssSelector: .bootstrap-switch-handle-off
  • 试试我分享的代码。 wait.until(ExpectedConditions.invisibilityOf(toggle));invisibilityOfElementLocated(By.cssSelector(".bootstrap-switch-handle-off"))); 之间有区别。
  • 好吧,ExpectedConditions 中没有名为 invisibilityOf 的方法,您说的是自定义方法吗?
  • import org.openqa.selenium.support.ui.ExpectedConditions; 中的标准条件。 seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/…
猜你喜欢
  • 1970-01-01
  • 2015-03-07
  • 2015-06-28
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 2013-12-25
  • 1970-01-01
相关资源
最近更新 更多