-1
<div class="dijitReset dijitInline dijitMenuItemLabel cpNavLeftLink dijitMenuItemSelected dijitMenuItem" data-dojo-attach-point="focusNode" role="menuitem" tabindex="-1" data-dojo-attach-event="onmouseenter:_onHover,onmouseleave:_onUnhover,ondijitclick:_onClick" aria-labelledby="dijit_PopupMenuBarItem_2_text" id="dijit_PopupMenuBarItem_2" aria-disabled="false" widgetid="dijit_PopupMenuBarItem_2" aria-haspopup="true" style="">*
    <span data-dojo-attach-point="containerNode" id="dijit_PopupMenuBarItem_2_text">Carload Tools</span>
</div>

I am trying to access the following element using selenium but for some reason it saying that they are not able to recognize this element please help. My code is working fine for other elements but for this one its not able to find the element.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
aman varma
  • 11
  • 4

1 Answers1

1

To identify the element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element_by_css_selector("div#dijit_PopupMenuBarItem_2[widgetid='dijit_PopupMenuBarItem_2']>span#dijit_PopupMenuBarItem_2_text[data-dojo-attach-point='containerNode']")
    
  • Using xpath:

    element = driver.find_element_by_xpath("//div[@id='dijit_PopupMenuBarItem_2' and @widgetid='dijit_PopupMenuBarItem_2']/span[@id='dijit_PopupMenuBarItem_2_text' and @data-dojo-attach-point='containerNode']")
    

Ideally, to locate a visible element you need to induce WebDriverWait for the 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, "div#dijit_PopupMenuBarItem_2[widgetid='dijit_PopupMenuBarItem_2']>span#dijit_PopupMenuBarItem_2_text[data-dojo-attach-point='containerNode']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='dijit_PopupMenuBarItem_2' and @widgetid='dijit_PopupMenuBarItem_2']/span[@id='dijit_PopupMenuBarItem_2_text' and @data-dojo-attach-point='containerNode']")))
    
  • 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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352