简体   繁体   English

无法在 Java 中使用 Selenium 捕获文本

[英]Not able to capture text using Selenium in Java

I am using Selenium in Java to capture values from the below code snippet.我在 Java 中使用 Selenium 从下面的代码片段中捕获值。 But it's returning a null value.但它返回一个空值。

 <div class="selenium" style="max-height: 0px !important; max-width: 0px !important;"> <div class="share metric"> <span id="nsf" class="nsf"> "2.2210636079249215+2.224479431183342+2.2244191794364805+2.2635962763351296+2.289855072463768+2.3309352517985613+2.3216783216783217+2.360018509949098+null+2.042235790730032+0.000006222369997702123+0</span></div>

I tried using我尝试使用

driver.findElement(By.xpath("//div[@class='share metric']/span[@id='nsf']")).getAttribute("value")

and

driver.findElement(By.xpath("//div[@class='share metric']/span[@id='nsf']")).gettext()

Your xpath is wrong, the span tag is not a child of <div class="share metric"> .您的 xpath 错误, span 标签不是<div class="share metric">的子标签。

Why using xpath at all?为什么要使用 xpath? Localting the element by it's id works just fine.通过它的id本地化元素工作得很好。

code:代码:

    WebElement nsf = driver.findElement(By.id("nsf"));
    String text = nsf.getText();
    System.out.println(text);

output:输出:

tarting ChromeDriver 101.0.4951.41 (93c720db8323b3ec10d056025ab95c23a31997c9-refs/branch-heads/4951@{#904}) on port 28658
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1655279888.338][WARNING]: This version of ChromeDriver has not been tested with Chrome version 102.
Čer 15, 2022 9:58:08 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
"2.2210636079249215+2.224479431183342+2.2244191794364805+2.2635962763351296+2.289855072463768+2.3309352517985613+2.3216783216783217+2.360018509949098+null+2.042235790730032+0.000006222369997702123+0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM