Java 在 Selenium WebDriver 上如何从 Span 标签获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19238272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:20:23  来源:igfitidea点击:

On Selenium WebDriver how to get Text from Span Tag

javahtmlseleniumselenium-webdriver

提问by Onu

On Selenium Webdriver, how I can retrieve text from a span tag & print?

在 Selenium Webdriver 上,如何从 span 标签中检索文本并打印?

I need to extract the text UPS Overnight - Free

我需要提取文本 UPS Overnight - Free

HTML code are as follow:

HTML代码如下:

div id="customSelect_3" class="select_wrapper">
<div class="select_display hovered">
<span class="selectLabel clear">UPS Overnight - Free</span>

Using following code:

使用以下代码:

String kk = wd.findElement(By.xpath(//*[@id='customSelect_3']/div[1]/span)).getText();
System.out.println(kk);

But above code is returning/printing text: 1.

但上面的代码正在返回/打印文本:1

回答by ddavison

Your code should read -

你的代码应该是——

String kk = wd.findElement(By.cssSelector("div[id^='customSelect'] span.selectLabel")).getText();

Use CSS. it's much cleaner and easier.. Let me know if that solves your issue.

使用 CSS。它更干净,更容易..让我知道这是否解决了您的问题。

回答by Sunita Venkatachalam

I agree css is better. If you did want to do it via Xpath you could try:

我同意 css 更好。如果您确实想通过 Xpath 执行此操作,则可以尝试:

    String kk = wd.findElement(By.xpath(.//*div[@id='customSelect_3']/div/span[@class='selectLabel clear'].getText()))

回答by NaviSaysListen

If you'd rather use xpath and that span is the only span below your div, use my example below. I'd recommend using CSS (see sircapsalot's post).

如果您更愿意使用 xpath 并且该跨度是 div 下的唯一跨度,请使用我下面的示例。我建议使用 CSS(请参阅 sircapsalot 的帖子)。

String kk = wd.findElement(By.xpath(//*[@id='customSelect_3']//span)).getText();

css example:

css示例:

String kk = wd.findElement(By.cssSelector("div[id='customSelect_3'] span[class='selectLabel clear']")).getText();

回答by carlin.scott

Maybe the span element is hidden. If that's the case then use the innerHtml property:

也许 span 元素被隐藏了。如果是这种情况,请使用 innerHtml 属性:

By.css:

通过.css:

String kk = wd.findElement(By.cssSelector("#customSelect_3 span.selectLabel"))
              .getAttribute("innerHTML");

By.xpath:

通过.xpath:

String kk = wd.findElement(By.xpath(
                   "//*[@id='customSelect_3']/.//span[contains(@class,'selectLabel')]"))
              .getAttribute("innerHTML");

"/.//" means "look under the selected element".

“/.//”的意思是“查看所选元素下方”。

回答by bhasat

String kk = wd.findElement(By.xpath(//*[@id='customSelect_3']/div[1]/span));

kk.getText().toString();

System.out.println(+kk.getText().toString());

回答by Shrikant Kakani

Pythonic way to get text from Span tags:

从 Span 标签获取文本的 Pythonic 方法:

driver.find_element_by_xpath("//*[@id='customSelect_3']/.//span[contains(@class,'selectLabel clear')]").text

回答by Sudheesh.M.S

PHP way of getting text from span tag:

PHP从span标签获取文本的方式:

$spanText = $this->webDriver->findElement(WebDriverBy::xpath("//*[@id='specInformation']/tbody/tr[2]/td[1]/span[1]"))->getText();

回答by Jayesh Vaghela

You need to locate the element and use getText() method to extract the text.

您需要定位元素并使用 getText() 方法来提取文本。

WebElement element = driver.findElement(By.id("customSelect_3"));
System.out.println(element.getText());