7

I'm learning Selenium and I have a question that, supposed we have something like below:

<div class='wrapper'>
  <div class='insider1'>
  <div class='insider2'>
<div class='wrapper'>
  <div class='insider1'>
  <div class='insider2'>

I can select a list of wrapper element using Css selector with .wrapper . So, suppose I have those elements, how do I select insider1 or insider2 using the wrapper WebElement that I've already had? I understand that there are many ways to select insider1 and insider2 but my question here is, Is it possible or not to select an inside element of a WebElement?

Thank you

2
  • Can you please revisit and clarify the question little bit more? Commented Jul 28, 2015 at 3:31
  • If your identify with Css selector or xpath of the given html code. That won't be identified on web page,b'coz there are two elements with same xpath/css selector. You should take xpath from its parent tag. Commented Jul 28, 2015 at 4:30

1 Answer 1

8

You haven't identified which language so I'm going to answer with examples in Java. You make the wrapper equal to a WebElement as follows (which you will get the first instance of back since you're using class instead of something unique but for the sake of argument lets say there's only one element with that class) and you should probably be closing your div's:

WebElement wrap = driver.findElement(By.className("wrapper"));

Then you can swap 'wrap' in, in place of your driver as a pointer and do the following to get at the dom tree elements

WebElement inside1 = wrap.findElement(By.xpath("div[1]"));
WebElement inside2 = wrap.findElement(By.xpath("div[2]"));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, this is the element that i'm looking for. I didn't know that WebElement has findElement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.