简体   繁体   English

Python Selenium 查找页面中是否存在属性等于某个值的元素

[英]Python Selenium Find if Element with an Attribute Equal to a Certain Value Exists in a Page

I am using Python and Selenium to scrape a web page.我正在使用 Python 和 Selenium 来抓取 web 页面。

I am trying to find the following 'next page' button in the page:我试图在页面中找到以下“下一页”按钮:

<button class="pagination__next" aria-label="click to go to the next page" disabled="true" aria-hidden="true"></button>

disabled='true' appears when I'm on the last page of a multi-page embedded javascript generated table.当我在多页嵌入式 javascript 生成表的最后一页上时出现disabled='true'

There may be more buttons on the page and some are disabled at the time I'm looking for this particular button.页面上可能有更多按钮,并且在我查找此特定按钮时某些按钮已禁用。

So what I'm trying to do is determine if this particular button which has class="pagination__next" with attribute disabled equals 'true' is in driver.page_source所以我要做的是确定这个具有class="pagination__next"且属性disabled等于'true'的特定按钮是否在driver.page_source

There are plenty of examples around about identifying the particular tag ('button').关于识别特定标签(“按钮”)的例子很多。 But not how to find the button and the button attribute disabled equal to 'true'但不是如何找到按钮disabled的按钮属性等于'true'

I tried various approaches.我尝试了各种方法。 I think the closes I got was:我认为我得到的关闭是:

driver.find_element_by_css_name('pagination__next[disabled='true'])

but I don't think it worked.但我认为它不起作用。

There is an identical question here but OP asks for Java solution. 这里有一个相同的问题,但 OP 要求 Java 解决方案。 I'm looking for Python.我正在寻找 Python。

Guidance please.请指导。

find_element_by_xpath() with multiple attributes is what you need, ie:具有多个属性的find_element_by_xpath()是您需要的,即:

driver.find_element_by_xpath('//*/button[@class="pagination__next"][@disabled="true"]')

Using .find_element_by_xpath with the AND expression:.find_element_by_xpathAND表达式一起使用:

driver.find_element_by_xpath('//button[@class="pagination__next" and @disabled="true"]')

With the AND expression you can locate element with more than one condition using the attribute value.使用AND表达式,您可以使用属性值定位具有多个条件的元素。

xpath-selenium OR & AND expression xpath-selenium OR & AND 表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM