Search code examples
pythonseleniumwebdriver

Click on a submenu with python selenium


Someone can help me please?

[menu and page]

1

I've tried find_element_by:

td: I can't find which is the correct id index;

xpath: its xpath change every page refresh;

class menuItem: brought the menu above;

text_link: no such element: Unable to locate element;

css td.menuItemSeleccionado: no such element: Unable to locate element;

classe menuItemSeleccionado: no such element: Unable to locate element

Someone has any idea?


Solution

  • Based on the provided page source the XPath for submenu could be like:

    "//td[contains(text(), 'Pesquisa Contratos')]"
    

    and in order to click it you have to:

    • 1 Click (or hover) the parent menu

      There might be a better way to locate the menu item, but you can try to use the partial text:

      to click:

      WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, 
      "//*[contains(text(), 'De Contratos E Cart')]"))).click()
      

      to hover:

      menu_item = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, 
      "//*[contains(text(), 'De Contratos E Cart')]")))
      ActionChains(driver).move_to_element(menu_item ).perform()
      
    • 2 Wait for the submenu element to be clickable and click it

      WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, 
      "//td[contains(text(), 'Pesquisa Contratos')]"))).click()
      

    imports

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