Change Value of Element within Python with JS and Selenium

Ranilyar

New member
Ok first of all I don't want to annoy anyone here I'm fairly new to coding and I'm well aware that asking in these forums should be your last place to go and only after you googled for a while. Anyway I have a problem. I tried to use 2captcha for my python web scraping automation. When I run the script I get the captcha token from 2captcha and in their API documentation they said, that I have to put the Token into the Value of the Element with fc-token inside (Screenshot)

1631257758310.png

Now I would like to place the token into the value="" of the html code and then submit.

This is what I've tried so far:

Code:
## Gets the captcha Token
time.sleep(15)
url=str(web.current_url)
solver = TwoCaptcha(APIKEY)

try:
    result = solver.funcaptcha(sitekey='B7D8911C-5CC8-A9A3-35B0-554ACEE604DA',
                               url=url,
                               surl='https://client-api.arkoselabs.com')
except Exception as e:
    sys.exit(e)

print(result['code'])
## The Captcha Token
token= result['code']

time.sleep(10)

##Switching to the right Iframe 
web.switch_to.frame('enforcementFrame')

##Here Im trying to change the value of the Element "FunCaptcha-Token" with JS
web.execute_script('''
  let {token} = arguments
  document.querySelector('FunCaptcha-Token').value = token
''', token)

Now when I run the script I get the token so the 2captcha part should work but the JS in Python part doesn't work. Thats the error code I get:

KeyError: 'document.getElementById("FunCaptcha-Token").value=87260ba9ceb681253.3489941501|r=us-ea [...] continues with the token

Now I'll add the HTML code.

<input type="hidden" id="FunCaptcha-Token" name="fc-token" value="91560ba8aabd2a839.9381249805|r=eu-west-1|metabgclr=%23ffffff|maintxtclr=%231B1B1B|mainbgclr=%23ffffff|guitextcolor=%23747474|metaiconclr=%23757575|meta=7|lang=de|pk=B7D8911C-5CC8-A9A3-35B0-554ACEE604DA|at=40|ht=1|atp=2|cdn_url=https%3A%2F%2Fclient-api.arkoselabs.com%2Fcdn%2Ffc|lurl=https%3A%2F%2Faudio-eu-west-1.arkoselabs.com|surl=https%3A%2F%2Fclient-api.arkoselabs.com">

1631257768622.png

Sorry for my long post I thank you in advance.
 

Holelibon

New member
It should look like:
Code:
driver.execute_script('''
  let {token} = arguments
  document.querySelector('#FunCaptcha-Token').value = token
''', token)

Then you want to submit the form or run the callback