2

I am executing javascript code in python and I have some troubles with the syntax. I suspect that the problem is with (") and (') symbols.

More explained:

In order to solve a "I'm Human" captcha, I have to insert a value in an html document. 1º I get a value:

value = get_value()

Now I have to insert that value (a string) into the html code of the webpage:

insert_solver = 'document.getElementByXpath("//textarea[@name="h-captcha-response"]").innerHTML="' + value + '";'   driver.execute_script(insert_solver)

When I execute the script I get this error:

selenium.common.exceptions.JavascriptException: Message: javascript error: missing ) after argument list

executing the script like in this example doesn't give any errors:

insertar_solucion = 'document.getElementById("g-recaptcha-response").innerHTML="' + respuesta_solver + '";'

So I suspect it's a problem with added (") and (') symbols

This is the result I want, from this enter image description here

To this: enter image description here

V-cash
  • 330
  • 3
  • 14

2 Answers2

3

There is no method execute it is execute_script instead.

You could do probably something like this :

element = driver.find_element_by_id("g-recaptcha-response")
driver.execute_script("yout JS stuff here with element[0]", element)

Udpate 1:

You can probably break down the strings like this :

element = driver.find_element_by_id("g-recaptcha-response")
inner_html = element.get_attribute('innerHTML')
final_str = inner_html + value;
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Yeah, it was a copy mistake, I used execute_script. The problem is when I execute the script where you say ("Your JS stuff here") I receive the same error. I have to put " and ' symbols in other way but I don't know how. I updated the questions – V-cash May 21 '21 at 16:10
  • @V-cash : update the code to bit feasible but since it's a captach, you should follow different approach. not sure if this is something you are looking `driver.execute_script("arguments[0].setAttribute('class','vote-link up voted')", element)` – cruisepandey May 21 '21 at 16:15
  • Actually I did somethong like that but without success. The thing is that I saw working perfectly very similar code. You can see it my update. inerstar_solucion = 'document.get.... And I appreciate your effort, thank you ;) – V-cash May 21 '21 at 16:23
  • I have never automated captcha but I would say to go through this https://stackoverflow.com/questions/58872451/how-can-i-bypass-the-google-captcha-with-selenium-and-python – cruisepandey May 21 '21 at 16:29
  • No, I know how to do it, I'm in the final step, I just have to insert a piece of code in the web. I update my sentence, This is where I get an error `insert_solver = 'document.getElementByXpath("//textarea[@name="h-captcha-response"]").innerHTML="' + value + '";'` – V-cash May 21 '21 at 16:41
2

I think this works just fine. It's tricky to pass on a string containing quotes.

pre = 'document.getElementByXpath('
x_path = '//textarea[@name="h-captcha-response"]'
post = f').innerHTML="{value}";'
# alternative: post = ').innerHTML="' + value + '";'
insert_solver = pre + x_path + post
driver.execute_script(insert_solver)
  • Now I get this error: `selenium.common.exceptions.JavascriptException: Message: javascript error: Unexpected token '}'` but I think this could be other thing and your answer is correct. – V-cash May 21 '21 at 17:15
  • hmm, sorry, I am not very aware of JS problems, but maybe `post = ').innerHTML="'+value+'";'` solves it, then. I'll edit the answer anyway, it may be helpful to others. – Pedro Kaneto Suzuki May 21 '21 at 19:14
  • Yeah I'm trying just that but at the moment the server is not responding well. I'm going to wait until tomorrow, thank you =) – V-cash May 21 '21 at 19:27
  • Oh, ok! Hope it works out. Have a nice weekend. – Pedro Kaneto Suzuki May 21 '21 at 19:46
  • Hey Pedro, it worked!! Thank you and have a nice day =)) ! – V-cash May 22 '21 at 08:47