【问题标题】:Unable click button by xpath in selenium webdriverselenium webdriver中的xpath无法单击按钮
【发布时间】:2016-11-26 20:42:03
【问题描述】:

这是我的代码

<button type="button" id="B-New" data-sap-ui="B-New" title="Add New Order" role="button" aria-disabled="false" tabindex="0" class="sapUiBtn sapUiBtnNorm sapUiBtnS sapUiBtnStd">Add</button>

xpath 这是"//*[@id='B-New']"

我试图点击按钮,但无法点击。

我用过xpath,ID类名什么的都不能点击。 这就是我正在使用的

driver.findElement(By.xpath("//*[@id='B-New']")).click()

请帮忙。

我也用过javascript

jse.executeScript("document.getElementById('B-New').click()");

但它也不起作用。

【问题讨论】:

  • 这是完整的代码
  • 无法点击是什么意思??有没有例外??也需要分享一下..
  • 是的,无法定位元素:{"method":"xpath","selector":"//*[@id='B-New']"}
  • 好的,我已经为您提供了答案,试试吧,让我知道结果...:)

标签: javascript java selenium-webdriver


【解决方案1】:

您应该尝试使用WebDriverWait 等到元素可见并启用点击如下:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("B-New")));
el.click();

注意:- 如果此元素在任何frame 内,则需要在使用driver.switchTo().frame("frame name or id") 查找并单击该元素之前切换该框架。

希望对你有帮助..:)

【讨论】:

  • 是的,我尝试过这种方式,但仍然无法单击按钮并显示错误无法找到元素
  • 这是抛出 NoSuchElementException 还是 TimeoutExcpetion??
  • 没有这样的元素异常
  • Saurabh Gaur 感谢您的支持,我可以使用 sikuli 点击按钮
  • @dimple 但是如果你得到 No such element 异常意味着你没有使用WebDriverWait 和我提供的答案来查找元素,因为如果你使用WebDriverWait 它会抛出TimeoutExxeption 而不是..so请按照我的例子使用它可能会有所帮助..谢谢
猜你喜欢
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 2014-01-04
  • 1970-01-01
  • 2019-02-04
  • 2017-01-17
相关资源
最近更新 更多