我正在嘗試讓使用 selenium 的程式在此 html 代碼上查找(然后單擊):
<svg aria-label="Add Photo or Video" class="_8-yf5 " color="#262626" fill="#262626" height="24" role="img" viewBox="0 0 48 48" width="24"></svg>
我將使用什么函式來查找此特定元素?謝謝
uj5u.com熱心網友回復:
請使用此 xpath
yourEle = driver.find_element_by_xpath("(//svg[@aria-label='Add Photo or Video'])")
uj5u.com熱心網友回復:
那是一個 svg 網路元素。您可以這樣定位(通過 xpath):
//*[name()='svg' and @aria-label='Add Photo or Video']
基本上有 4 種方法可以在 Selenium 中單擊。
我將使用這個 xpath
//*[name()='svg' and @aria-label='Add Photo or Video']
代碼試用1:
time.sleep(5)
driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']").click()
代碼試用2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @aria-label='Add Photo or Video']"))).click()
代碼試用3:
time.sleep(5)
button = driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']")
driver.execute_script("arguments[0].click();", button)
代碼試用4:
time.sleep(5)
button = driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']")
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
PS:請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313827.html
