I need some help with pressing this button. In this page the button "cersa" is untraceable with selectors.
https://www.consiglionazionaleforense.it/ricerca-avvocati
I also used JS code that in the console it works but in the python script it crashes with:
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read properties of undefined (reading 'click')
the js script:
js_code = "button = document.getElementsByClassName('btn-link btn-link--green mt-4'); button[0].click();"
driver.execute_script(js_code)
This should work
driver.execute_script("document.getElementsByClassName('btn-link btn-link--green mt-4').click()")
I am not sure why you are intending to go with execute_script, but if you can go with the direct selenium, then you may use this:
driver.find_element(By.XPATH, "//div[@id='ricerca-avvocati']//button[@type='submit']").click()
UPDATE:
I did a keen testing on the issue of why the button is not clicking even though the element is found in the DOM.
The issue is, the element is not clickable at all, unless you click on the I am not a robot checkbox, which if clicked by automation (or a bot) may not work (as websites detect the bot actions). Try clicking on the reCaptcha I am not a robot checkbox, and if you are not detected as a bot, then the element would become clickable and the action would be fulfilled.
I used regular click function - the one that I gave you as a second code block, and it works, but I had to manually force click on the reCaptcha checkbox - only then it worked. As you can see, the code exited with 0 code, which mean no errors. But the caveat is the reCaptcha handling, which is quite harder with a bot/selenium
Process finished with exit code 0