如何从Selenium WebDriver,Java中的标签中获取价值
我有HTML代码:
Samsung Galaxy SII (I9100)
... ...
我希望使用Selenium 2(WebDriver)从/ h3>标签获得三星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;
根据您是否有其他具有“描述”类的div元素,您可能需要进一步微调结果。
以防万一:
为了找到页面上作为描述以供进一步使用的所有div,您可以这样做:
/** Declare XPath variable **/ string descriptionElementXPath = "//div[contains(@class, 'description')]"; /** Grab all description div elements **/ List 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"));
- 关于Java重载和动态绑定的问题
- Play类中的方法表单(Class )在Play中已弃用! 骨架
- DocumentBuilder.parse()线程安全吗?
- 在java上绘制图形
- 有没有时间你不会使用递归?
- javax.validation.constraints.Pattern注释的参数化错误消息?
- Eclipse + Maven + JavaServer Faces – > ClassNotFoundException:StartupServletContextListener
- 当两个应用程序都使用嵌入式activemq时,如何将Jms消息从一个spring-boot应用程序发送到另一个应用程序
- 如何在xml java中更新节点值