Windstorm1981
Windstorm1981

Reputation: 2680

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.

Upvotes: 2

Views: 1029

Answers (2)

frianH
frianH

Reputation: 7563

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

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98901

find_element_by_xpath() with multiple attributes is what you need, i.e.:

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

Upvotes: 1

Related Questions