簡體   English   中英

元素是可見的,但webdriver無法找到

[英]Element is visible but cannot be found by webdriver

我正在使用selenium和python進行自動化測試,並且在嘗試單擊web中的元素時遇到問題。

我正在使用find_element_by_xpath,提供正確的路徑(我已嘗試使用瀏覽器並返回1中的1)。

driver = webdriver.Chrome()
driver.get('page_url')
driver.find_element_by_xpath(//button[@class="aoc-end md-button md-ink-ripple"])

這是html:

<div class="_md md-open-menu-container md-whiteframe-z2 md-active md-clickable" id="menu_container_77" style="top: 499px; left: 866px; transform-origin: right top;" aria-hidden="false">
 <md-menu-content class="agent-dropdown-menu-content new-menu-content" width="3" role="menu"><!----><md-menu-item>
    <button class="aoc-end-work md-button md-ink-ripple" type="button" ng-transclude="" ng-disabled="!agent.capabilities.canSupervisorLogout" ng-click="logOutAgent(agent.userHandle)" role="menuitem">

應該找到該元素,但實際結果是selenium.common.exceptions.ElementNotVisibleException:Message:元素不可見

在角度開發的Web應用程序中查找定位器非常棘手,尤其是當開發人員不遵循自動化角度的任何有用指南時,例如為每個Web元素等添加唯一屬性...

在你的情況下,似乎發生了同樣的事情。

以下定位器應該適合您(順便說一句,您在driver.find_element_by_xpath()方法中定位器之前和之后錯過了“”):

driver.find_element_by_xpath("//button[@ng-click='logOutAgent(agent.userHandle)']");

從例外情況看,該元素似乎存在於頁面中但目前不可見。 元素不可見的可能原因可能是(元素被另一個元素掩蓋,元素可以在下拉列表中等)。 如果element在下拉列表中,則首先打開列表,然后找到該元素。

希望它會有所幫助。

由於您嘗試單擊的元素是動態元素,因此在單擊它之前需要等待它變為可單擊。 使用預期條件和WebDriver等待:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//button[@class='aoc-end md-button md-ink-ripple']"))).click()

另外,請注意外部和內部xpath選擇器上的" "' '引號" "

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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