Reputation: 151
I am stuck at one end, I am not able to get text value.
For better view :-
<div class="">
<span class="address_city">Glenwood</span>
<span class="address_state">GA</span>
<span class="address_zip xh-highlight">30428</span>
</div>
I identify class address_zip...
using following xpath :
//*[contains(text(),'30428')]
How can I get text value GA and Glenwood?
Upvotes: 1
Views: 5420
Reputation: 2611
Try these below mentioned xpath
Get value of GA
//span[text()= '30428']/..//preceding-sibling::span[text()= 'GA')]
Explanation of xpath:- Use text
method along with <span>
tag and move ahead with another <span>
tag using preceding-sibling keyword
.
OR
//span[text()= 'Glenwood']/following-sibling::span[text()= 'GA']
Explanation of xpath:- Use text
method along with <span>
tag and move ahead with another <span>
tag using following-sibling keyword
.
OR
//span[text()= 'Glenwood']/..//following-sibling::span[text()= 'GA']
Explanation of xpath:- Use text
method along with <span>
tag and move ahead with another <span>
tag using following-sibling keyword
.
Get value of Glenwood
//span[text()= 'GA']/..//preceding-sibling::span[text()= 'Glenwood']
Explanation of xpath:- Use text
method along with <span>
tag and move ahead with another <span>
tag using preceding-sibling keyword
.
Upvotes: 0
Reputation: 128
//[contains(text(),'30428')]/preceding-sibling::span[contains(@class,'address_city')] //[contains(text(),'30428')]/preceding-sibling::span[contains(@class,'address_state')]
Upvotes: 0
Reputation: 23805
It won't work , coz there is duplicate class name for same , Thats why I ask that is there any way to point from text() to upper two element.
You can use below xpath
:-
To get Glenwood text :
.//div[span[text() = '30428']]/span[@class = 'address_city']
To get GA text :
.//div[span[text() = '30428']]/span[@class = 'address_state']
Upvotes: 2
Reputation: 50809
You can use preceding-sibling
//*[contains(text(),'30428')]/preceding-sibling::span[@class='address_city']
//*[contains(text(),'30428')]/preceding-sibling::span[@class='address_state']
You can also locate the zip code element and use it
WebElement zip = driver.findElement(By.xpath("//*[contains(text(),'30428')]"));
String city = zip.findElement(By.xpath("//preceding-sibling::span[@class='address_city']")).getText();
String state = zip.findElement(By.xpath("//preceding-sibling::span[@class='address_state']")).getText();
Upvotes: 1
Reputation: 9101
Why do not you just use:
string city =Driver.FindElement(By.ClassName("address_city")).Text;
string state =Driver.FindElement(By.ClassName("address_state")).Text;
in case these classes are duplicated for other elements:
//first get the zip as you are doing now.
IWebElement zip=Driver.FindElement(By.Xpath("//*[contains(text(),'30428')]"));
//now get the father element.
IWebElement father=zip.FindElement(By.XPath(".."));
//now get all the spans
IList<IWebElement> allElements=father.FindElements(By.TagName("span"));
//reach the elements you want.
IWebElement city=allElements.ElementAt(0);
IWebElement state=allElements.ElementAt(1);
Upvotes: 0