【问题标题】:Facing issue while clicking dropdown button using selenium webdriver code使用 selenium webdriver 代码单击下拉按钮时面临问题
【发布时间】:2017-08-21 12:56:09
【问题描述】:

我正在使用的应用程序有几个具有相同 xml 代码的下拉按钮。以下是所有下拉按钮的类似代码。

<button class="btn dropdown-toggle bs-placeholder btn-default" role="button" data-toggle="dropdown" type="button" data-id="invContactList" title="" data-original-title="Nothing selected">

我使用下面的代码来点击按钮

driver.findElement(By.xpath("//button[@data-toggle='dropdown']")).click();

使用此代码应用程序仅成功运行一次,但在此之后出现错误:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令交互。我已删除浏览器 cookie,但它无济于事。

有人可以帮我解决根本原因和解决方案吗?

【问题讨论】:

  • 请发布一些按钮的 HTML。你试过像“button[data-id='invContactList']”这样的 CSS 选择器吗?你确定你正在找到你想要的元素吗?有可能您的定位器找到了多个,而第一个可用的被隐藏了。
  • 你试过我的答案了吗?
  • 是的,它的工作原理......谢谢

标签: java html selenium-webdriver


【解决方案1】:

您可能需要等待该元素:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-toggle='dropdown']"))).click();

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-id='invContactList']"))).click();

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@data-toggle='dropdown']"))).click();

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@data-id='invContactList']"))).click();

在你最后尝试一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多