Reputation: 1990
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
Upvotes: 7
Views: 8853
Reputation: 512
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]"));
Upvotes: 8