[英]How to extract partial text from an element seperated by <br> tag through selenium?
How to separate 70 from 70 (100%) where 70 and (100%) are linked by <br>
tag in selenium? 如何从70(100%)中分离70 ,其中70和(100%)通过硒中的
<br>
标签链接?
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 我得到的输出是:7
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. Xpath太疯狂了,但是要解决您的问题,我认为这可能与在split()函数中的引号之间添加空格一样简单。 It should be
它应该是
REP=Repor.split(“ “)
Edit after clarification: the string Repor is being set with a line break. 澄清后进行编辑:字符串Repor带有换行符。 Thus the following code should find "70" and "(100%)" individually.
因此,以下代码应分别找到“ 70”和“(100%)”。
String lines[] = Repor.split("\\r?\\n");
then lines[0]
is "70" and lines[1]
is "(100%)". 那么
lines[0]
为“ 70”, lines[1]
为“(100%)”。 Is this what you wanted? 这就是你想要的吗?
To extract the text 70 you can use either of the following Locator Strategies : 要提取文本70 ,可以使用以下定位策略之一 :
cssSelector : 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 : 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();
每个标签都被视为父标签存在的文本节点的分隔符:决定是指定所需文本节点的索引//a/text()[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.