Py学习  »  Python

在selenium python中查找特定元素

nefariousbehaviour • 3 年前 • 1252 次点击  

我正在尝试让我的程序使用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>

我将使用什么函数来查找这个特定元素? 谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131013
 
1252 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Jayanth Bala
Reply   •   1 楼
Jayanth Bala    3 年前

请使用这个xpath

yourEle = driver.find_element_by_xpath("(//svg[@aria-label='Add Photo or Video'])")
cruisepandey
Reply   •   2 楼
cruisepandey    3 年前

这是一个svg web元素。您可以(通过xpath)这样定位:

//*[name()='svg' and @aria-label='Add Photo or Video']

点击Selenium基本上有4种方式。

我将使用这个xpath

//*[name()='svg'和@aria label='Add Photo或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

附言: 请办理登机手续 dev tools (谷歌浏览器)如果我们有 唯一的 报关进口 HTML DOM 或者不是。

检查步骤:

Press F12 in Chrome ->去 element 第节->做一个 CTRL + F ->然后粘贴 xpath 看看,如果你愿意的话 要素 越来越 突出显示 具有 1/1 匹配节点。