【问题标题】:Data not capturing using Selenium web driver未使用 Selenium Web 驱动程序捕获数据
【发布时间】:2016-06-15 10:17:31
【问题描述】:

我正在尝试使用 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>

Web 驱动程序代码

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();

【问题讨论】:

  • 我想要段落中的字符串 - 最大的优点:我们的新配方现在有助于加强皮肤自身的水分屏障。更多的水分留在里面。皮肤感觉柔软,有弹性。拥有健康的光泽。
  • 感谢@RemcoW,我试过了,但与此段落相同的结果在输出中仍然不可见。您认为可以提供帮助的任何其他方式。
  • 我已经编辑了我的答案,请看一下。欢迎消息阻止我收到文本,这也可能是您的问题。

标签: java html selenium


【解决方案1】:

我刚刚在 Python 中对其进行了测试,如果您将 By.id("inspirational_copy").tagName("p") 替换为有效的 css 选择器,则可以使用 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()

【讨论】:

  • 我认为这不是问题(欢迎消息),因为我能够从中获取其他数据,唯一的问题是我正在寻找的这一段。如果你能设法得到这个文本,你能用java试试吗?
  • 不幸的是,我无法在 Java 中尝试它。你有没有尝试过这个建议。试一试也无妨。
  • 非常感谢@RemcoW。使用您提供的代码关闭欢迎消息后,我现在可以看到这一段。 'WebElement close = driver.findElement(By.cssSelector("button[title='Close']"); close.click()'
  • 这个想法成功地关闭了欢迎信息。问题解决了:)
  • 很高兴你能成功。请接受我的回答,表明这个问题已经解决。
【解决方案2】:

你可以通过使用 xpath 作为定位器轻松解决这个问题

driver.findelement(By.xpath("//div[@id='inspirational_copy']/p"));
String description1 = description.getText();

希望这会得到你想要的文本。

【讨论】:

  • 我已经尝试过了,但不幸的是没有得到输出。如果这有效并为您提供所需的输出,您是否在最后对其进行了测试。您在哪个浏览器中进行测试。
  • 欢迎信息有问题。首先编写脚本来关闭它,然后使用 gettext() 方法。我正在使用 firefox 46 它正在工作。
  • 哪个 selenium 版本与 Firefox 46 兼容?
  • 硒 2.53 和火狐 46
【解决方案3】:
String description1 = driver.findElement(By.xpath("//*[@id="inspirational_copy"]/p")).getText();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    相关资源
    最近更新 更多