0

I am attempting to click a button on a html page using Python and selenium web driver.

This is the source code of the page http://pastebin.com/112g1Gje.

EDIT: The relevant part at is at the end.. "btn btn-primary"

I am really close to figuring this out: The class name had spaces in it and I replaced the spaces with the dots... that made it work (Thanks to another SO post).

driver.find_element_by_css_selector(".btn.btn-primary").click()

Now the error I get is:

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

So I am trying something like:

driver.execute_script('document.querySelector("btn.btn-primary").click()')

From my understanding I have to write a javascript that clicks the button instead because the element is not visible. But that doesn't work.

The error I get is almost unreadable:

driver.execute_script('document.querySelector("btn.btn-primary").click()')
  File "/Users/Richie/anaconda/lib/python3.5/site-      packages/selenium/webdriver/remote/webdriver.py", line 461, in execute_script
    {'script': script, 'args':converted_args})['value']
  File "/Users/Richie/anaconda/lib/python3.5/site-     packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "/Users/Richie/anaconda/lib/python3.5/site-  packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message:    document.querySelector(...) is null

Anyone know?

  • Wow, that HTML source is REALLY hard to read. You might make it easier on us to show just the html for the relevant element you want to click. Also, check to see if there are more than one elements that are returned matching that css selector I see at least two of them. One has an id, which would be better to use. the css selector for that would be "button#button-send-feedback" – Breaks Software Jun 04 '16 at 01:45
  • Whoops! Yeah I know man! It's nuts! It's all the way at the end. CTRL+F "btn btn-primary" –  Jun 04 '16 at 01:55
  • That's also the first button, that's the feedback button. I'm trying to click the message button. –  Jun 04 '16 at 02:04
  • In your first attempt, the error itself tells you what's wrong. There is something else lying on the element you want to click. ie a CSS error. The second attempt using javascript indicates a javascript error on your site. You will have to fix these two using the chrome dev tools before you can use selenium. – e4c5 Jun 04 '16 at 02:16
  • Solved it, thanks guys. I'll post the solution now. –  Jun 04 '16 at 02:30

1 Answers1

1
driver.find_element_by_css_selector(".btn.btn-lg.btn-block.btn-message.open-write-message").click()

I was clicking the wrong thing. Make sure to include the dots.