【问题标题】:How to click elements with no ID如何单击没有 ID 的元素
【发布时间】:2019-08-27 14:08:14
【问题描述】:

我有以下 HTML,我正在尝试单击名为 tasks 的元素。

*<td class="class_popup2_menuitem_caption" unselectable="on" nowrap="">Tasks</td>*

我试过这个:

x = driver.find_element_by_xpath("//div/table/tbody//td[contains(text(), 'Tasks')]") 
x.click()

但是,我收到以下回复。

ElementNotVisibleException:消息:元素不可交互

【问题讨论】:

  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。

标签: python html selenium


【解决方案1】:

我正在尝试使用 selenium 和 java 展示此异常的根本原因。请尝试在python中实现。

ElementNotVisibleException: Message: element not interactable 是在找到一个元素但您无法与之交互时引起的。例如,您可能无法单击或发送密钥。

这可能有几个原因:

 - The element is not visible / not displayed The element is off screen
 - The element is behind another element or hidden Some other action
 - needs to be performed by the user first to enable it.

可能有助于使其可交互的策略(取决于具体情况。)

1.等到元素可见/可点击

WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOf(element)); 
wait.until(ExpectedConditions.elementToBeClickable(element));

2.滚动直到元素在显示范围内

Actions action = new Actions(driver);
action.moveToElement(element);

3.使用 javascript 直接与 DOM 交互

JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("var element = document.querySelector('locator'); element.value = 'whatever';")

4.执行任何其他必要的操作,并可能等到那之后。

【讨论】:

  • 我了解您正在尝试提供帮助,但以另一种语言发布答案,而不是 OP 要求的 against the rules
猜你喜欢
  • 1970-01-01
  • 2021-02-23
  • 2018-09-04
  • 2014-10-02
  • 2016-01-02
  • 2021-04-24
  • 2013-10-30
  • 1970-01-01
  • 2021-12-28
相关资源
最近更新 更多