Selenium: Not able to understand xPath(Selenium:无法理解 xPath)
问题描述
我有一些这样的 HTML:
I have some HTML like this:
<h4 class="box_header clearfix">
<span>
<a rel="dialog" href="http://www.google.com/?q=word">Search</a>
</span>
<small>
<span>
<a rel="dialog" href="http://www.google.com/?q=word">Search</a>
</span>
</h4>
我正在尝试使用 Selenium 在 Java 中获取 href.我尝试了以下方法:
I am trying to get the href here in Java using Selenium. I have tried the following:
selenium.getText("xpath=/descendant::h4[@class='box_header clearfix']/");
selenium.getAttribute("xpath=/descendant::h4[@class='box_header clearfix']/");
但这些都不起作用.它一直抱怨我的 xpath 无效.谁能告诉我我做错了什么?
But none of these work. It keeps complaining that my xpath is invalid. Can someone tell me what mistake I am doing?
推荐答案
你应该使用getAttribute来获取链接的href.您的 XPath 需要对最终节点的引用以及所需的属性.以下应该有效:
You should use getAttribute to get the href of the link. Your XPath needs a reference to the final node, plus the required attribute. The following should work:
selenium.getAttribute("xpath=/descendant::h4[@class='box_header clearfix']/a@href");
您还可以修改您的 XPath,使其更改更加灵活,甚至使用 CSS 来定位元素:
You could also modify your XPath so that it's a bit more flexible to change, or even use CSS to locate the element:
//modified xpath
selenium.getAttribute("//h4[contains(@class,'box_header')]/a@href");
//css locator
selenium.getAttribute("css=.box_header a@href");
这篇关于Selenium:无法理解 xPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Selenium:无法理解 xPath


基础教程推荐
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01