你好,我是 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
还必须包含 img
的 notification-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/