Search code examples
javascriptpythonseleniumselenium-webdriverweb-inspector

slelnium python : clicking a dynamic object/element which has no id or name


when Right click is performed then an options popup/screen appears and its a dynamic content/element.

This is the Options Popup:

enter image description here

This dynamic content/element disappears as soon as the mouse is clicked somewhere else on the screen even if its clicked on the inspect element window the dynamic content/element disappears

there are few option to select from on the popup but in the html code there is not name or id to these options , so how can i access/select them from selenium using python ?

This is How the HTML looks :

enter image description here

i intend to select the option HMH NC by sending the Righ click using selenium and then making select the option but since there is no name or id for the options i can figure out how to do it


Solution

  • You'll have to search for it by text. I would do something like this (untested code):

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By    
    
    item_labels = WebDriverWait(my_driver,5).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "div.item span.label")),
        message="failed to find any menu items")
    
    target = None
    for item in item_labels:
        if item.get_attribute("text") == target_value:
            target = item
            break
    target.click()