0

I'm trying to cycle through several listitems contained on a webpage. I can't seem to successfully fetch these elements. The elements I'm trying to fetch are defined as such, and there are 4 in the page:

<div data-test-component="StencilReactView" role="listitem" class="css-1uk1gs8">

I'm using the following in an attempt to fetch these elements (using Python): driver.find_elements(By.XPATH, "//div[@role='listitem']")

Once I'm able to successfully fetch this element I can cycle through them as I need to, but I can't seem to get it to successfully fetch. Any help would be greatly appreciated.

I've tried fetching the element by XPATH, as well as by class, without success.

2 Answers2

0

Given the HTML:

<div data-test-component="StencilReactView" role="listitem" class="css-1uk1gs8">

The elements seem to be dynamic elements.


Solution

To fetch the elements you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "td.entity-name")))
    
  • Using XPATH:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@role='listitem']")))  
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

maybe you should use the full xpath to locate element, it is absxpath

abs-xpath like this /html/body/div[3]/div/div/div/div/div[2]/form/input, which from the https://ecas.ec.europa.eu/cas/login website

you should write codes like this:

driver.find_element(By.XPATH, r"/html/body/div[3]/div/div/div/div/div[2]/form/input")
WebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.XPATH, r"/html/body/div[3]/div/div/div/div/div[2]/form/div[2]/div[2]/button")))
driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div/div/div[2]/form/div[2]/div[2]/button").click()

besides, you can use another method to locate element, like:

driver.find_elements(By.CLASS_NAME, "username-input")[0]
driver.find_elements(By.TAG_NAME, 'a')[0]
driver.find_elements(By.ID, 'userSkin')[0]

the last, maybe you should use EC.* of selenium to wait for something happen you click and html reload, then some element can be located like login or logout some website, you should wait until page reload and some element can be located.

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.visibility_of_all_elements_located((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.presence_of_element_located((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.title_contains((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.text_to_be_present_in_element((By.XPATH, r"")))
Jiu_Zou
  • 463
  • 1
  • 4