0

Hi I tried to send javascript that look like this :

$("img[src|='']").attr('src', result.responseJSON.data);

It's to change image with null value to new value. I'm using execute script so that it looks like this :

driver.execute_script("$("img[src|='']").attr("src", result.responseJSON.data);}});")

and

driver.execute_script('$("img[src|='']").attr('src', result.responseJSON.data);}});')

but it always return an error like

enter image description here

or

enter image description here

I also signing it to a python variable and call it with execute script but no luck, any hint to fix it? Thanks in advance.

Rikudo Pain
  • 441
  • 9
  • 22
  • Two thoughts: 1/ Does it work from devtools command line? - `execute_script` == `command line` (just operated a bit differently), if you can run on there you can get it to work in the other 2/ what is img? is that an object you want to pass in? – RichEdwards Sep 02 '20 at 18:14
  • Img is a html tag, so i want to insert value to – Rikudo Pain Sep 02 '20 at 18:31
  • Your null tag selection is correct but the way you call the execute script is completely off. – Arundeep Chohan Sep 02 '20 at 23:39
  • And how the correct way? – Rikudo Pain Sep 03 '20 at 02:15
  • Do you need to define which image tag in particular? And did you try it in the console first? – RichEdwards Sep 03 '20 at 06:05
  • do you need to use jquery? - as your question has tagged selenium i can give you a selenium answer - You just need your specific webelement identified and you can do `.setAttribute("myAttriute","myValue")` – RichEdwards Sep 03 '20 at 09:51
  • @RichEdwards yes, already test it and it works perfectly. Yes it can but it would take long process to get from Network XHR to selenium because the content or the picture that I want to set as value (result.responseJSON.data) are taken from server response and only visible in Network XHR. It's already hard to get the data shown to the console. – Rikudo Pain Sep 03 '20 at 12:22

1 Answers1

0

Finally I found the answer, I should put escape char before like :

driver.execute_script("$(\"img[src|='']\").attr("src", result.responseJSON.data);")

and it works.

Rikudo Pain
  • 441
  • 9
  • 22