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
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.
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.
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]"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With