简体   繁体   中英

How to do find aria-label using selenium and python

This is my code-->

element = driver.find_elements_by_css_selector("[aria-label='收回讚']")

It doesn't work, it creates an None type. Any help is appreciated! Thanks in advance!

update:

my update code:

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

这是html的参考

To find the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[aria-label='收回讚']")))
  • Using XPATH :

     element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@aria-label='收回讚']")))
  • Note : You have to add the following imports:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

References

You can find a couple of relevant detailed descroptions in:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM