简体   繁体   中英

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.

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.

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

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'

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. I'm looking for Python.

Guidance please.

find_element_by_xpath() with multiple attributes is what you need, ie:

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

Using .find_element_by_xpath with the AND expression:

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.

xpath-selenium OR & AND expression

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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