【问题标题】:Unable to extract the text using Xpath无法使用 Xpath 提取文本
【发布时间】:2015-12-31 15:25:06
【问题描述】:

请帮助找出这里有什么问题。我尝试使用下面给出的 Xpath,但没有运气。请看图片。

  1. 我尝试使用自定义 xpath -

xpath("//*[@class=' js flexbox canvas canvastext webgl no-touch geolocation postmessage no-websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients no-cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache svg inlinesvg smil svgclippaths']/body/div[@class='ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable']/div[@class='ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix ui-draggable-handle']/span[@class='ui-dialog-title']").getText

这个返回元素未找到错误。但是,当在 Selenium IDE 中给出此 Xpath 时,会识别该对象

  1. id("ui-id-8").getText - 这个返回空白

【问题讨论】:

  • edit您的问题并使其可读。此外,切勿将图像用于代码或标记;始终使用格式化为代码的文本。谢谢。
  • 嗨..对不起..我对堆栈溢出不太熟悉。因此不知道如何正确格式化。
  • 您是否查看了How do I format my posts using Markdown or HTML?上的帮助中心页面
  • 嗨..感谢分享链接。但是他们允许 HTML 标签吗?我浏览了链接,发现只允许使用几个标签。或者有没有其他方法可以共享 HTML?
  • 将问题简化为minimal reproducible example,并将简化后的 HTML 直接包含在您的问题中,格式为代码。

标签: java selenium xpath extract


【解决方案1】:

更简单的方法是按照屏幕截图中给出的 html 使用 xpath。 (高亮的 span 元素的 id 是 'ui-id-7',但是你在第二点使用了 'ui-id-8',它是一个空的 UL。

您正在搜索“ui-dialog-title”的第一个点 xpath,它应该是“ui-dialog-titlebar”。但是你尝试的xpath太麻烦,可能不正确。

为了让您在标题栏中获取文本,以下代码 sn-p 应该会有所帮助:

driver.findElement(By.xpath("//div[@id='ui-id-7']")).getText();

【讨论】:

  • 嗨..看起来在最新代码部署后 HTML 发生了变化。昨天是 ui-id-8。很抱歉造成混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
相关资源
最近更新 更多