2

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.

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is this the only way? I (mis?)read elsewhere that xpath was not the ideal way to locate an element. In addition, if the site owner changes the page structure won't xpath blow up?
No it's not the only way, you can select all buttons, then filter by css and lastly by disabled attribute, but xpath can be very efficient and precise locating elements, I guess it depends on how you use it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.