html - 如何在 xpath 上找到正确的链接?

标签 html css selenium xpath selenium-ide

你好,我是 selinium ide 和 xpath 的新手,这就是我需要这方面帮助的原因。我需要点击网站上的一些链接我可以让它在 1 个链接中工作我不知道它将如何搜索并点击另一个链接因为它有不同的编号和帖子标题。链接看起来像这个。

http://imageshack.com/a/img27/328/zv17.png

<a href="/sample/15151-hey-you">post</a>
<a href="/sample/142151-im-okay">post</a>
<a href="/sample/512512-thats-fine">post</a>

我使用这个 xpath,它适用于第一个链接

//div[@id='main']/ul[2]/li[1][@class='notification-posted']/a[2]

将单击 1 个链接和前面的链接的正确 xpath 是什么

请帮我解决这个问题

编辑
非常感谢您的第一个代码有效,但第二个代码无效。但是 ul 中的每个帖子都很重要,您的代码正在处理 ul 中的第一篇帖子。

<h5>20 seconds ago</h5>
<ul>
    <li class="notification-posted">
        <img height="15" alt="" src="/assets/images/icons/notification-posted.png" />
        <a href="/account/54351-wews">wews</a>
        send new
        <a href="/news/53235">post</a> <!--//li[@class='notification-posted'][1]/a[2]-->
    </li>
</ul>
<h5>3 minutes ago</h5>
<ul>
    <li class="notification-posted">
        <img height="15" alt="" src="/assets/images/icons/notification-posted.png" />
        <a href="/account/632323-yokol">yokol</a>
        submitted a new
        <a href="/news/253129-loss">post</a> <!--//li[@class='notification-posted'][2]/a[2]-->
    </li>
</ul>
<h5>4 minutes ago</h5>
<ul/>
<h3>6 minutes ago</h3>
<ul/>
<h5>7 minutes ago</h5>
<ul>
    <h2>8 minutes ago</h2>
    <ul />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <img height="15" alt="" src="/assets/images/icons/notification-posted.png" />
    <a href="/account/153316-problem">hey</a>
    send new
    <a href="/news/25151-helloworld">post</a> <!--***Problem was here***-->
</ul>

应该是

//li[@class='notification-posted'][6]/a[2] 

对吧?但它解析其他链接。谢谢你的回答。

最佳答案

/ul[2]...应该返回第二个帖子的第一个帖子 ul

假设每个 ul 的帖子数量既可变又不重要(即你想要帖子而不管它们在哪个ul),那么你可以绕过ul .

如果<li class='notification-posted'>唯一标识帖子,您可以引用以下3个链接:

(//li[@class='notification-posted'])[1]/a[2]
(//li[@class='notification-posted'])[2]/a[2]
(//li[@class='notification-posted'])[3]/a[2]
(// ... )[n]/a[2]

a[2]是必需的,因为您在每个实例中都在第二个链接之后。

如果这不够有选择性,您可以添加过滤器 li还必须包含 imgnotification-posted.png图标始终与超链接相关联:

(//li[@class='notification-posted'][img src="/assets/images/icons/notification-posted.png"])[1]//a[2]

链接也一样[2][3]

编辑

您可以找到所有包含文本 post 的链接使用以下 xpath:

//a[@href][normalize-space(.)='post']

使用以下 xsl 在(xhtml 化的 html 版本)上测试:

<xsl:for-each select="//a[@href][normalize-space(.)='post']">
    <Link>
        <xsl:value-of select="@href"/>
    </Link>
</xsl:for-each>

给予:

<Link>/news/53235</Link>
<Link>/news/253129-loss</Link>
<Link>/news/25151-helloworld</Link>

关于html - 如何在 xpath 上找到正确的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970218/

相关文章:

html - 基本 CSS 问题

python - 出现错误 :(Element not interactable) when sending keys into the input field in selenium python

css - 无法在 IE 中设置 HTML5 元素的样式(尽管有 shiv 并显示 :block)

php - if 语句中的变量未正确实现

javascript - 在页面上的每个 img 元素之后运行一段脚本

javascript - 使用 CSS : Absolute Positioning Conversation to Bottom while Maintaining Scroll 重新创建电话短信 UI

html - 为什么 HTML5 中的导航栏做成列表?

jquery - chrome 中带省略号的多行文本问题

html - 如果可能,我如何使用 selenium 来识别嵌套框架中的元素?

javascript - 如何在没有 ID (Java) 的 Selenium 中单击 Javascript 按钮?