簡體   English   中英

如何點擊沒有ID的元素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM