Home > database >  Selenium (python) not finding element which clearly exists
Selenium (python) not finding element which clearly exists

Time:01-24

I am trying to click through the levels of a site's navigation using python and selenium. The navbar contains list items that have subelements within them.

Here is the html of the navbar. enter image description here

The objective here is to find the element with id="ts_time", to hover over it and to click on the element within it.

So far I have tried the following selection types: ID, XPath, Class_Name

Here is the ID.

time_menu_button = driver.find_element(By.ID, "ts_time")
ActionChains(driver).move_to_element(time_menu_button)

time.sleep(2.5)

This results in a NoSuchElementException

*** Corrected - the ID name of the element

CodePudding user response:

As I can see you are trying to find element with id "imgLogo". But it does not exists on provided html. So selenium could not find anything.

CodePudding user response:

To hover over the element with id="ts_time" you need to invoke perform() and then to click on the element within it you can use either of the following locator strategies:

time_menu_button = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li#ts_time")))
ActionChains(driver).move_to_element(time_menu_button).peform()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li#ts_time > a"))).click()
  • 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
    
  • Related