Search code examples
pythonpython-3.xseleniumselenium-webdriverwebdriver

Selenium times out when trying to click <a> tag


I am trying to use selenium to click a button it works fine on every page except on 1 single page and on 1 single element in that page. I have extracted it an made a reproducible example. I have tried everything I can think of but the item never gets clicked. I have observed the console with no luck, if i click it manually it works.

Here is my index.html:

<html>
  <head>
     <title>Test</title>
  </head>
  <body>
    <a id="go" href="javascript:void(0);" class="button_next font_two" onclick="nextStep()">Confirm
    </a>

    <script>
      function nextStep() {
        console.log("moving...")
      }
    </script>
  </body>
</html>

Here is what i have tried to automate it

    nextBtn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "go"))
    )

    nextBtn.click()

From above the error I get is timeout

I also tried to do this

...
nextBtn.send_keys(u'\ue007')
...

but also a timeout

then i tried to do it a bit differently

nextBtn = driver.find_element_by_id("go")
...

This time I get element not interactable error

I tried using XPATH instead but no luck, I have also tried adding driver.implicitly_wait(20) as well as getting a different element from page before doing this and also no luck. at this point I ran out of ideas.


Solution

  • You should try to this, .execute is to execute a driver command where as .execute_script is to execute JavaScript code.

    So, doing this should work for you :

    javaScript = "document.getElementById('go').click();"
    driver.execute_script(javaScript)
    
    # OR Directly calling that function which will be called after click
    
    javaScript ="nextStep()"
    driver.execute_script(javaScript)
    

    You can learn more about it from execute and execute_script .