Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

Selenium trying to get elements


I'd like to get the elements by Selenium as the attached pic:

ess-cell class="data-numeric"

However, the following code didn't work even though:

find_element_by_tag_name('div')

part works correctly. Does anyone know why?

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

enter image description here


Solution

  • To get the elements <ess-cell class="data-numeric"...> you can use either of the following Locator Strategies:

    • Using css_selector:

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

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

    Ideally you have to induce WebDriverWait for presence_of_all_elements_located() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

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

      elements = WebDriverWait(row, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "./div/div//ess-cell[contains(@class, 'data-numeric')]")))
      
    • 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