我有以下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:消息:元素不可交互
我试图使用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.执行必要的其他操作,并可能等到此之后。