Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Get WebElement inside a WebElement

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

like image 785
Kiddo Avatar asked Jul 27 '15 23:07

Kiddo


People also ask

How do you find the WebElement inside another WebElement?

You can find another element inside an element provided that element is child element of that element. something like this: WebElement eleParent=driver. findElement(Locator); WebElement eleChild=eleParent.

How do you get the content inside a tag in Selenium?

Selenium offers a getText() method used to get the text of an element, i.e.; it can be used to read text values of an element from a web page.


1 Answers

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]"));
like image 62
Fi Horan Avatar answered Sep 22 '22 12:09

Fi Horan