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"));
Author by
bpk
Updated on July 26, 2022Comments
-
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 almost 9 yearsI guess you meant to write
//div[@class='menuBar']//i[@class='menuItem']
as xpath? -
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 almost 9 yearsIn that case you need to use * for tag independent search as shown by @SiKing
-
bpk almost 9 years@aholt if the element comes under more than 1 div then how will you make the nested cssSelector expression?
-
aholt almost 9 yearsnot sure what your meaning is. maybe update your question with an example of what you are asking?