Selenium 试图获取元素

Selenium trying to get elements

我想通过Selenium获取元素,如附图:

ess-cell class="data-numeric"

但是,即使以下代码也不起作用:

find_element_by_tag_name('div')

部分工作正常。有谁知道为什么?

row.find_element_by_tag_name('div').find_element_by_tag_name('div').find_elements_by_tag_name('ess-cell')

要获取元素 <ess-cell class="data-numeric"...>,您可以使用以下任一方法 :

  • 使用css_selector:

    elements = row.find_elements(By.CSS_SELECTOR, "div > div ess-cell.data-numeric")
    
  • 使用 xpath:

    elements = row.find_elements(By.XPATH, "./div/div//ess-cell[contains(@class, 'data-numeric')]")
    

理想情况下你必须诱导 WebDriverWait for and you can use either of the following :

  • 使用CSS_SELECTOR:

    elements = WebDriverWait(row, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div > div ess-cell.data-numeric")))
    
  • 使用 XPATH:

    elements = WebDriverWait(row, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "./div/div//ess-cell[contains(@class, 'data-numeric')]")))
    
  • 注意:您必须添加以下导入:

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