我想單擊包含 selenium python 中的類和標題的元素。
網頁包含沒有任何 id 但具有唯一名稱的可重復類。一旦加載到頁面中,我想檢測并單擊此標題“PaymateSolutions”。下面是html標簽。我嘗試了很多方法,但最終還是出現了錯誤。僅供參考,我不能按類使用 find 元素,因為它們不是唯一的。
<div class="MuiGrid-root MuiGrid-item" title="PaymateSolutions">
<p class="MuiTypography-root jss5152 MuiTypography-body1">PaymateSolutions</p>
</div>
我嘗試使用 XPATH 根據標題獲取驅動程式元素的幾種方法
方法一:-
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//class[@title='PaymateSolutions']")))
方法2:-
element2 = (WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "//p[@title='PaymateSolutions']")))
)
方法三:-
element2 = (WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "//[@title='PaymateSolutions']")))
)
有人可以在這里幫忙嗎?
uj5u.com熱心網友回復:
對于方法 1 -title是div標簽的屬性。因此,Xpath 將類似于以下內容:
//div[@title='PaymateSolutions']
對于方法 2 -p標簽沒有title屬性。PaymateSolutions是text的的p標簽。Xpath 應該是這樣的:
//p[text()='PaymateSolutions']
對于方法 3 - Tag Namexpath 中沒有。Xpath 將是:
//*[@title='PaymateSolutions']
Or
//div[@title='PaymateSolutions']
參考鏈接 - Link1 , Link2
我們可以Explicit waits像下面這樣申請:
# Imports required for Explicit waits:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get(url)
wait = WebDriverWait(driver,30)
payment_option = wait.until(EC.element_to_be_clickable((By.XPATH,"xpath for PaymateSolutions option")))
payment_option.click()
用于參考顯式等待的鏈接-鏈接
uj5u.com熱心網友回復:
您一直在嘗試的所有 XPath 似乎都有點錯誤。請使用以下 XPath :
//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']
代碼試用1:
time.sleep(5)
driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']").click()
代碼試用2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']"))).click()
代碼試用3:
time.sleep(5)
button = driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']")
driver.execute_script("arguments[0].click();", button)
代碼試用4:
time.sleep(5)
button = driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']")
ActionChains(driver).move_to_element(button).click().perform()
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329945.html
上一篇:硒中沒有xpath點擊
