使用Selenium Web驱动程序无法捕获数据

时间:2016-06-15 10:17:32

标签: java html selenium

我正在尝试使用ID和标记名从网页捕获产品说明,但是当我打印它时,它显示为空白。但是我想我已经使用了正确的定位器来定位元素。

页面来源

    <div id="item-description-block" class="layout-container layout-container-background clearfix">
<h2>About this item</h2>
<div id="social_share" class="details-row" onclick="javascript:clickPDP('Share','123070751499');">
<div class="details-row clear_both">
<div id="inspirational_copy">
<p>The big plus: Our new formula now helps strengthen skin's own moisture barrier. More moisture stays in. Skin feels soft, springy. Has a healthy-looking glow.</p>
</div>

网络驱动程序代码

driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.id("inspirational_copy").tagName("p"));
String description1 = description.getText();

3 个答案:

答案 0 :(得分:0)

我刚刚在Python中对其进行了测试,如果您使用有效的css选择器替换By.id("inspirational_copy").tagName("p"),则可以使用getText()来获取您要查找的文本。

driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.cssSelector("div[id='inspirational_copy']>p"));
String description1 = description.getText();

当我到达页面时,我注意到了一条欢迎辞。此消息阻止我获取文本。关闭它之后,我可以毫无问题地获得元素和文本。

WebElement close = driver.findElement(By.cssSelector("button[title='Close']");
close.click()

答案 1 :(得分:0)

使用xpath作为定位器

可以轻松解决此问题
driver.findelement(By.xpath("//div[@id='inspirational_copy']/p"));
String description1 = description.getText();

希望这能得到你想要的文字。

答案 2 :(得分:0)

String description1 = driver.findElement(By.xpath("//*[@id="inspirational_copy"]/p")).getText();