2

How to separate 70 from 70 (100%) where 70 and (100%) are linked by <br> tag in selenium?

Here's the structure:

<a href="/report/campaigns/698/sent" class=""> 70<br> (100%)</a>

Code that I am using is :

String[] REP=new String[1];
String Repor= driver.findElement(By.xpath("//html[1]/body[1]/section[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[5]/table[1]/tbody[1]/tr[1]/td[6]")).getText();

REP=Repor.split(""); 

System.out.println("REP "+REP[0]);

Output that i am getting is : 7

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ashu
  • 43
  • 5
  • 1
    Hi! When you were asking your question, there was a big orange **How to Format** box to the right of the text area with useful info in it. There was also a toolbar full of formatting aids. And a **[?]** button giving formatting help. *And* a preview area showing what your post would look like when posted, located between the text area and the Post Your Question button (so that you'd have to scroll past it to find the button, to encourage you to look at it). Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – T.J. Crowder Mar 16 '19 at 14:16
  • dhilmathy beat me to fixing it for you this time. Please do use the tools next time. Thanks! – T.J. Crowder Mar 16 '19 at 14:17
  • 1
    We can't help you without seeing the HTML/DOM structure you're trying to get this information from. – T.J. Crowder Mar 16 '19 at 14:17
  • Please use the "edit" link, not comments, to improve the question. – T.J. Crowder Mar 16 '19 at 14:31

3 Answers3

2

That Xpath is crazy, but to address your issue I think it might be as simple as adding a space between the quotes in your split() function. It should be

REP=Repor.split(“ “)

Edit after clarification: the string Repor is being set with a line break. Thus the following code should find "70" and "(100%)" individually.

String lines[] = Repor.split("\\r?\\n");

then lines[0] is "70" and lines[1] is "(100%)". Is this what you wanted?

C. Peck
  • 3,641
  • 3
  • 19
  • 36
1

To extract the text 70 you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement myElement = driver.findElement(By.cssSelector("a[href='/report/campaigns/698/sent']"));
    String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].firstChild.textContent;', myElement).toString();
    
  • xpath:

    WebElement myElement = driver.findElement(By.xpath("//a[@href='/report/campaigns/698/sent']"));
    String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].firstChild.textContent;', myElement).toString();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Each tag is considered a separator for exist text-node of the parent tag: decision is to specify an index of desired text-node //a/text()[1]

fpsthirty
  • 185
  • 1
  • 4
  • 15