2

I want to extract vale between parent and child node using Xpath. How to achieve that?

**<div class="Shareaccess">
    Value to be extract
       <a href="javascript:shareAccessValue()" id="shareKey">
             KEY
       </a>
       <span></span>
  </div>**

How to get text value output as 'Value to be extract'? It should not be 'Value to be extract KEY'. 'KEY' should be ignored.

Guy
  • 46,488
  • 10
  • 44
  • 88

2 Answers2

2

You can remove the child text from the result

Java syntax

String allText = driver.findElement(By.className("Shareaccess")).getText();
String childText = driver.findElement(By.id("shareKey")).getText();
String parentText = allText.replace(childText, "");

parentText will be "Value to be extract"

Guy
  • 46,488
  • 10
  • 44
  • 88
  • But is it possible to create single xpath which will ignore child text from the result without using java functions? Only xpath should ignore the contents. – Sanket Chandak Jan 27 '16 at 10:06
  • @SanketChandak No, selenium doesn't support it currently. Another option is to use [JavaScript](http://stackoverflow.com/a/19040341/5168011) – Guy Jan 27 '16 at 10:12
  • @SanketChandak What do you mean? Selenium API doesn't support it, there isn't really much more information I can give you. – Guy Jan 27 '16 at 11:43
0

Try this

C#:

string parentText = driver.FindElement(By.XPath("//a[@id='shareKey']/parent::div").Text().Replace(driver.FindElement(By.XPath("//a[@id='shareKey']").Text(),"");
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
  • yes, saw it coming so i changed the code try the new one its like in the other example but in one line and less variable to define. – Leon Barkan Jan 27 '16 at 10:27