Selenium - Find a child element under a DIV

12,109

Well, depending on what language you're using, the method call will be different, but the selector should be the same across language bindings:

css:

"div.menuBar .menuItem"

xpath:

"//div[@class='menuBar']//*[@class='menuItem']"

In java, the call would look like this:

driver.find(By.cssSelector("div.menuBar .menuItem"));
Share:
12,109
bpk
Author by

bpk

Updated on July 26, 2022

Comments

  • bpk
    bpk almost 2 years

    Can someone help me with the below HTML:

    <div id="ext-156" class="menuBar">
      <a id="ext-234" href="javascript:void(0);" class="active">
        <i id="ext-365" class="menuItem"></i>
      </a>
    </div> 
    

    I am looking for the element with class "menuItem" and only from inside the div with class "menuBar" in Selenium.

  • Saifur
    Saifur almost 9 years
    I guess you meant to write //div[@class='menuBar']//i[@class='menuItem'] as xpath?
  • aholt
    aholt almost 9 years
    @Saifur you can do the xpath selector without the tag name. It might require a wildcard (*) though....been a while since I've used xpath.
  • Saifur
    Saifur almost 9 years
    In that case you need to use * for tag independent search as shown by @SiKing
  • bpk
    bpk almost 9 years
    @aholt if the element comes under more than 1 div then how will you make the nested cssSelector expression?
  • aholt
    aholt almost 9 years
    not sure what your meaning is. maybe update your question with an example of what you are asking?