如何使用 selenium 和 python 查找 aria-label
How to do find aria-label using selenium and python
这是我的代码-->
element = driver.find_elements_by_css_selector("[aria-label='收回讚']")
它不起作用,它创建了一个 None
类型。任何帮助表示赞赏!
提前致谢!
更新:
我的更新码:
try:
element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//*[aria-label='收回讚']")))
#check if unlike path exisit
except:
element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//*[aria-label='讚']")))
element.click()
#if not find like path and click it
找到需要归纳的元素WebDriverWait for the visibility_of_element_located()
and you can use either of the following :
使用CSS_SELECTOR
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[aria-label='收回讚']")))
使用XPATH
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@aria-label='收回讚']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到一些相关的详细说明:
这是我的代码-->
element = driver.find_elements_by_css_selector("[aria-label='收回讚']")
它不起作用,它创建了一个 None
类型。任何帮助表示赞赏!
提前致谢!
更新:
我的更新码:
try:
element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//*[aria-label='收回讚']")))
#check if unlike path exisit
except:
element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//*[aria-label='讚']")))
element.click()
#if not find like path and click it
找到需要归纳的元素WebDriverWait for the visibility_of_element_located()
and you can use either of the following
使用
CSS_SELECTOR
:element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[aria-label='收回讚']")))
使用
XPATH
:element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@aria-label='收回讚']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到一些相关的详细说明: