繁体   English   中英

如何从中获取价值 <h3> Java Selenium WebDriver中的标记

[英]How to get value from <h3> tag in Selenium WebDriver, Java

我有html代码:

<div class="description">
<h3><strong>Samsung</strong> Galaxy SII (I9100)</h3>
<div class="phone_color"> ... </div>
...
</div>

我想使用Selenium 2(WebDriver)从/ h3>标签获取值Samsung Galaxy SII(I9100

有人知道怎么做吗?

这是用C#编写的,但是将其转换为Java并不难:

/** Declare variables **/
string descriptionTextXPath = "//div[contains(@class, 'description')]/h3";

/** Find the element **/
IWebElement h3Element = driver.FindElement(By.XPath(descriptionTextXPath));

/** Grab the text **/
string descriptionText = h3Element.Text;

根据是否具有“ description”类的其他div元素,您可能需要进一步微调结果。


以防万一:

为了在页面上找到所有用作进一步使用说明的div,您可以执行以下操作:

/** Declare XPath variable **/
string descriptionElementXPath = "//div[contains(@class, 'description')]";

/** Grab all description div elements **/
List<IWebElement> descriptionElements = driver.FindElements(By.XPath(descriptionElementXPath ));

然后,您可以使用forloop遍历每个元素并获取所需的数据。

以下是上述C#代码到Java的转换:

/ **声明变量** /

String descriptionTextXPath = "//div[contains(@class, 'description')]/h3";

/ **找到元素** /

IWebElement h3Element = driver.findElement(By.xpath(descriptionTextXPath));

/ **抓取文字** /

String descriptionText = h3Element.toString();

要么,

String descriptionText = h3Element.getText();
WebElement divElement = driver.findelement(By.classname("description"));
String str = divElement.getText();
System.out.println(str);

这是完美的..它有所帮助..谢谢:)

WebElement divElement = driver.findelement(By.classname("description"));
String str = divElement.getText();
enter code here

str包含值。

webDriver.findElement(By.tagName("h3"));

暂无
暂无

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

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