2

From the code below I would like to extract: "I give you €0.00.".

With the following xpath I can target it from Developer tools:

//*[@id="windowID"]/text()[2]

But using Selenium I can only look for this element with driver.findElement(By.Xpath(""))

The problem is that it says that the result of xpath expression is not an element, but an [object Text]

If I target only the ancestor div (windowID), then the whole text is received. I only want the mentioned part to be saved to a string.

<div id="windowID" class="windowClass">"Your are awesome."<br>"I give you €0.00."<br>"But you should give me €0.00."<br>"Would you like to do that?"</div>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Illes
  • 39
  • 4

2 Answers2

1

To extract the text I give you €0.00. you can use the following line of code :

IWebElement elem = driver.FindElement(By.XPath("//div[@class='windowClass' and @id='windowID']"));
string text = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].childNodes[3].textContent;", elem);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Here I get all the text and split the text by <br> and we can print the text by separating.

 String mytext=driver.findElement(By.xpath("//div[@id='windowID'].getAttribute("innerHTML");
    String[] separatestring= mytext.split(Pattern.quote("<br>"));
    String firstone= separatestring[0];
    String secondtone= separatestring[1];
    System.out.println(firstone);
    System.out.println(secondtone);

I did it by java, Here you can convert it to C#. Hope it will help you.

Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52