Selenium:无法理解 xPath

编程入门 行业动态 更新时间:2024-10-24 23:22:04
本文介绍了Selenium:无法理解 xPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些这样的 HTML:

I have some HTML like this:

<h4 class="box_header clearfix"> <span> <a rel="dialog" href="www.google/?q=word">Search</a> </span> <small> <span> <a rel="dialog" href="www.google/?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

本文发布于:2023-07-27 14:04:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Selenium   xPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!