4

so I am doing some web scraping using Selenium with Python and I am having a problem. I am clicking a Next button to move to the next page on a certain website, but I need to stop clicking it when I reach the last page. Now, my idea of doing it would be just to use some_element.click() in a try/except statement and wait until it gives me an error while the button is not clickable anymore. It seems though, .click() doesn't emit any signal of any kind, it doesn't throw an error when the button can't be clicked and it doesn't emit any true or false signal.

a code snippet I tried using:

while True:
    try:
       button = driver.find_element_by_class_name('Next_button')
       button.click()
    except:
       break

Is there any other way? Thanks and cheers.

6
  • Is the element displayed when you reach the last page? Commented Jun 7, 2015 at 19:01
  • 1
    It could be that the link is still clickable, but instead of moving to the next page, it does nothing. Can you share the link to the website you are running the code against? Commented Jun 7, 2015 at 19:02
  • @Saifur is right. Check what html attributes change for that button when you reac the last page. Commented Jun 7, 2015 at 19:02
  • Saifur, the Next page button is still shown, but it is greyed out on the last page. @Vikas Neha Ojha, the class name changes at the last page and so does the xpath to it. How can I detect this change in Python? Or did I understand your advice wrongly. alecxe, yes, that could be true. The loop just goes on forever. Commented Jun 7, 2015 at 19:16
  • 1
    The loop just goes on forever - Please mention whether it is even navigating through the next webpages. You mentioned that class name changes and I can see that you are trying to find the element by class name only, so still your code should work. Could you please post the html of just the button on initial page and the last page? Commented Jun 7, 2015 at 19:19

2 Answers 2

6

Without knowing more about your example target, the minimal that can be said is that a clickable attribute would have an 'href' attribute.
You can use the get_attribute property of an element:

button = driver.find_element_by_class_name('Next_button')
href_data = button.get_attribute('href')
if href_data is None:
  is_clickable = False

Gets the given attribute or property of the element.

This method will first try to return the value of a property with the given name. If a property with that name doesn’t exist, it returns the value of the attribute with the same name. If there’s no attribute with that name, None is returned.

Values which are considered truthy, that is equals “true” or “false”, are returned as booleans. All other non-None values are returned as strings. For attributes or properties which do not exist, None is returned.

More on Using get_attribute

You could also try is_displayed or is_enabled

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

6 Comments

I am pretty sure this will solve my problem. I do not have the full code right next to me, but I will test it soon. Only one small thing, the class changes its name at the last page. I'll try to test the code soon. Thanks a lot.
If you know for certain that the class name changes on the "last page" you could also perform a check to see if elements with that given class name exist: stackoverflow.com/questions/7991522/…
Sorry it took me so long, I had to rewrite the code until that point (since I don't have the source code of my application now). I wanted to try your first suggestion, but I have a new problem. Whenever I use find_element_by_class_name I get an error: "Message: invalid selector: Compound class names not permitted". Basically, because the name of the class has a space in it, "copy-light next". Any quick suggestion? Thanks.
Try just "next" instead of "copy-light next", the partial string should catch. Otherwise try to catch the object a bit differently, see here: stackoverflow.com/questions/17808521/…
Still working, and a good answer to this day.
|
4

Use this to get classes element.get_attribute("class") and check if list of classes contains characteristic class (for example "disable") from your html framework which is used to describe unclickable buttons

2 Comments

Thank you, I will test this too. I did not know getAttribute() could be used in such a way.
+1 for this answer. Selenium introduced expected conditions with element_to_be_clickable wait function, but for some unknown reason it deemed a button clickable even though it was disabled. Getting the class attribute and searching for 'disabled' solved it for me.

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.