如何在XPath选择中包含属性

12 投票
1 回答
14829 浏览
提问于 2025-04-18 13:50

我正在使用selenium来浏览网页。

以下是页面的源代码:

<td colspan="10" align="right">
                <input type="button" name="previous" value="Previous" onclick="this.form.action = 'coca-cola-north-america-group-company-37452.htm?previous=true&currentPage=6';this.form.submit();" > 
                <input type="hidden" name="currentPage" id="currentPage" value="6" />

                <input type="button" name="next" value="Next" onclick="this.form.action = 'coca-cola-north-america-group-company-37452.htm?next=true&currentPage=6';this.form.submit();" >
            </td>


<td colspan="10" align="right">
                <input type="button" name="previous" value="Previous" onclick="this.form.action = 'coca-cola-north-america-group-company-37452.htm?previous=true&currentPage=7';this.form.submit();" > 
                <input type="hidden" name="currentPage" id="currentPage" value="7" />

我使用selenium的点击方法来点击下一页,

browser.find_element_by_xpath('//*[@name="next"]').click()                  

现在,当我到达最后一页时(下面是源代码),我发现下一页的链接的xpath是一样的---但是它的最后有一个disabled属性。我想让selenium在这里停止。

不过,我不太确定怎么在xpath中加入disabled属性。

                <input type="button" name="next" value="Next" onclick="this.form.action = 'coca-cola-north-america-group-company-37452.htm?next=true&currentPage=7';this.form.submit();" disabled>
            </td>

1 个回答

36

试试这个:

//*[@name="next"][not(@disabled)]

撰写回答