Search code examples
pythonseleniumubuntutor

How to click a button in Tor Browser with Selenium and python


I use Tor Browser with Selenium to automate a click on a button.

File script.py

from tbselenium.tbdriver import TorBrowserDriver
with TorBrowserDriver("/home/user/Selenium/tor-browser_en-US/") as driver:
driver.get('https://www.example.com/form.html')

How do I manage to perform a click on this button (excerpt from the HTML file)?

<form method="post" id="IdA" action="https://example.com/action.php"><input id='valid' name='valid' value='012.23945765955' type="hidden"><button class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" data-callback="onSubmit" id="IdA" style="background:url(https://www.example.com/button.gif);width:190px;height:58px;border:none;cursor:pointer;display:none;" type="submit"></button></form>

I tried this, but it did not work:

driver.findElement(By.Id("IdA")).click()

Solution

  • I'm assuming you are trying to bypass a CAPTCHA.

    You can do this one of two ways. You can click the button by using a selector. For example, an XPath selector for a button with class "g-recpatcha". You can also just execute JavaScript code on the page to call the onSubmit() function.

    So two options are:

    1. driver.find_element_by_xpath("//button[@class='g-recaptcha']").click()

    2. driver.execute_script("onSubmit("" + captchaToken + "")")

    See the reCAPTCHA callback on 2captcha API, Solving Captchas.