- How to locate the element in web page that has common children and no unique attribute to identify.
- Date: 3/15/2016
- Ex: <div class="wrapper">Hi I am here</div> <div class ="Wrapper"> Hi I am there </div>.
- Like this if we have multiple <div> in the page with same attribute class value Wrapper, how will you identify specific text from it. I know you could use [1] or [2].... based on the nth number of <div> tag but sometimes even that one will not work.
- Have same scenario here:
- Source: http://stackoverflow.com/questions/36018524/selenium-get-a-text-node-using-xpath-and-use-it-as-a-string-in-java/36020432#36020432
- I have a section of code like this:
<div id='wrapper'>
<span>*</span>
Question 1
</div>
- I want to store the Question 1 as a string in Java so when I use the xpath //div/text() to get the text "Question 1." However when I try to print the value, I am getting the error Invalid Selector Exception Xpath //div/text() is: [object Text]. It should be an element.
String text = driver.findElement(By.xpath("//div")).getText();
//returns *Question 1
String text = driver.findElement(By.xpath("//div/text()")).getText();
//returns error
- How am I supposed to store just the Question 1. I can replace the * once I get it using the first method above but I don't want to do that.
Ex1:
- This is MSN website, here I would like to find out the text : Todd Palin in intensive care after snowmobile accident. How to find it, looks similar to the above scenario right because even this has code as below:
- <img alt="In this file photo, former Alaska Gov. Sarah Palin of Alaska, Republican vice pr..." data-src="{"default":"//img-s-msn-com.akamaized.net/tenant/amp/entityid/BBqu9TY.img?h=75&w=100&m=6&q=60&u=t&o=t&l=f&f=jpg&x=524&y=269"}" src="http://img-s-msn-com.akamaized.net/tenant/amp/entityid/BBqu9TY.img?h=75&w=100&m=6&q=60&u=t&o=t&l=f&f=jpg&x=524&y=269" title="todd palin - Andrew Harrer/Bloomberg News" class =" loaded" data-initial-set="true" width="100">
- <span class="title">Todd Palin in intensive care after snowmobile accident</span>
- Solution:
- I’m not
sure I know the best way to do this but this is a way. The issue is that you
are looking for specific text within a group of elements that are children of
the wrapper
DIV
. The only thing I can see in the example that you provided is that the text you want is outside of a containing tag. We can use that to our advantage. Basically the approach is to grab the entirety of the text inside wrapper and then loop through all child elements of wrapper removing the text contained within each from the original string. That should leave the text that you want. I have tested the code below using the example you provided.
String wrapperText = driver.findElement(By.id("wrapper")).getText().trim(); // get all the text under wrapper
List<WebElement> children = driver.findElements(By.cssSelector("#wrapper > *")); // get all the child elements of wrapper
for (WebElement child : children)
{
String subText = child.getText(); // get the text from the child element
wrapperText = wrapperText.replace(subText, "").trim(); // remove the child text from the wrapper text
}
System.out.println(wrapperText);
No comments:
Post a Comment