[英]How to click elements with no ID
我有以下 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. 执行任何其他必要的操作,并可能等到此后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.