这是一个 svg 网页元素。您可以像这样定位(通过 xpath):
//*[name()='svg' and @aria-label='Add Photo or Video']
在 Selenium 中基本上有 4 种点击方式。
我将使用这个 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:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。
检查步骤:
Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654335@匹配节点。