0

I can't find the element of the html below.

<span class="tabComboBoxName" id="tab-ui-id-1565209097494" aria-hidden="true">20/07/2019</span>

I've tried the following codes:

elem = browser.find_elements_by_xpath("//class[@id='tab-ui-id-1565209097494']")
elem = browser.find_elements_by_class_name('tabComboBoxName')
elem = browser.find_elements_by_id('tab-ui-id-1565209097494')

For those tries I got an empty list.

2 Answers2

1

The element is a dynamically generated element so to locate the element you need to induce WebDriverWait for the desired 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, "span.tabComboBoxName[id^='tab-ui-id-']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='tabComboBoxName' and starts-with(@id, 'tab-ui-id-')][contains(., '20/07/2019')]")))
    
  • 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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0
  1. Make sure that the element doesn't belong to an <iframe>, if it does - you will need to switch_to() the iframe where the element lives prior to attempting to find it
  2. Make sure that the element doesn't belong to ShadowDOM, if it does - you will need to locate the relevant ShadowRoot element, cast it to the WebElement and use find_element() function of the WebElement instead of driver
  3. Make sure to use Explicit Wait as it might be the case the element is not immediately available and it's being loaded later via AJAX request
  4. Try using another locator strategy, for instance you can stick to the element text like:

    //span[text()='20/07/2019']
    
Dmitri T
  • 119,313
  • 3
  • 56
  • 104