简体   繁体   中英

formatting java script in python selenium

I want to inject key into the innerHTML of a webpage through selenium The injection script sample is document.getElementById("g-recaptcha-response").innerHTML="TOKEN_FROM_2CAPTCHA";

key is a variable which contains the return code

I've tried driver.execute_script(document.getElementById("g-recaptcha-response").innerHTML = + key, ';')

I thought this was the most logical looking way to do it but it show errors even before it runs

You need to pass a string as a parameter of driver.execute_script()

key = "TOKEN_FROM_2CAPTCHA"
driver.execute_script(f'document.getElementById("g-recaptcha-response").innerHTML = {key};')

I like to use f strings but you can use string concatenation as well.

I found a work around that worked for me

driver.execute_script("document.getElementById('g-recaptcha-response').style.display = 'block';")
driver.find_element(By.ID, 'g-recaptcha-response').send_keys(key)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM