如何单击没有ID的元素

时间:2019-04-05 15:56:45

标签: python html selenium

我有以下HTML,我试图单击名为task的元素。

*<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:消息:元素不可交互

1 个答案:

答案 0 :(得分:-2)

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

ElementNotVisibleException:消息:元素不可交互是在找到元素但无法与它交互时引起的。例如,您可能无法单击或发送密钥。

可能有以下几种原因:

 - 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。执行其他必要的操作,并可能等到之后。

相关问题