如何从<h3>获取价值Selenium WebDriver 中的标签,Java [英] How to get value from <h3> tag in Selenium WebDriver, Java
问题描述
我有 html 代码:
<h3><strong>三星</strong>Galaxy SII (I9100)</h3><div class="phone_color">... </div>...
我想使用 Selenium 2 (WebDriver) 从/h3> 标签获取 Samsung Galaxy SII (I9100) 值
有人知道怎么做吗?
这是用 C# 编写的,但将其转换为 Java 应该不难:
/** 声明变量 **/string descriptionTextXPath = "//div[contains(@class, 'description')]/h3";/** 查找元素 **/IWebElement h3Element = driver.FindElement(By.XPath(descriptionTextXPath));/** 抓取文本 **/字符串 descriptionText = h3Element.Text;
根据您是否有其他带有 'description' 类的 div 元素,您可能需要进一步微调您的结果.
<小时>以防万一:
为了找到页面上所有用作描述以供进一步使用的 div,您可以这样做:
/** 声明 XPath 变量 **/string descriptionElementXPath = "//div[contains(@class, 'description')]";/** 抓取所有描述 div 元素 **/列表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();
I have html code:
<div class="description">
<h3><strong>Samsung</strong> Galaxy SII (I9100)</h3>
<div class="phone_color"> ... </div>
...
</div>
I want to get the value Samsung Galaxy SII (I9100) from /h3> tag using Selenium 2 (WebDriver)
Any one know how to do it?
This is written in C#, but it shouldn't be difficult to convert it over to 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;
Depending on whether you have other div elements with the 'description' class, you may need to further fine tune your results.
Just in case:
In order to find all divs on the page that act as descriptions for further use, you can do this:
/** Declare XPath variable **/
string descriptionElementXPath = "//div[contains(@class, 'description')]";
/** Grab all description div elements **/
List<IWebElement> descriptionElements = driver.FindElements(By.XPath(descriptionElementXPath ));
You can then use a forloop to go through each element and grab the data you need.
The following is the conversion of above C# code to 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.toString();
OR,
String descriptionText = h3Element.getText();
这篇关于如何从<h3>获取价值Selenium WebDriver 中的标签,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!