1

I have the problem of running this. I get back an error stating that I have a syntax issue. Ineed help with the Syntax.

driver.execute_script('$('select[name='condition'] option:eq(30)').prop('selected', true);') ^ SyntaxError: invalid syntax

driver.execute_script("$('select[name='condition'] option:eq(30)').prop('selected', true);")
Ahmad Ishaq
  • 11
  • 1
  • 5
  • @AhmadIshaq This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – undetected Selenium Jan 15 '20 at 20:08
  • You could build the javascript function that jquery calls and then call the jquery function but I have not yet figured it out and you might as well just use javascript. Here is the jquery link https://code.jquery.com/jquery-3.4.1.js – Jortega Jan 15 '20 at 20:31
  • one of the syntax problems here is the unbalanced quotes... you're using single quotes inside of single quotes inside of double quotes.... so 'select[name=' is one quote for open, one quote for close, then condition' is a syntax issue... you can build a string to send to execute_script which makes things a bit more clear... then inside of your string assign js vars to use inside of name= bit... also note that jQuery must be loaded and ready before using "$" – pcalkins Jan 15 '20 at 21:26
  • @DebanjanB I want to select a value from dropdown but cant select it using selenium so i tried jquery to do that but now i can execute the script but still cant select the required field. Following is the html code for it: – Ahmad Ishaq Jan 16 '20 at 07:13
  • condition
    – Ahmad Ishaq Jan 16 '20 at 07:13

2 Answers2

2

So if jquery is not on the page you need to add it first. I am just appending it to the page below. Then run your code.

driver.execute_script("""
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js';
document.head.appendChild(script);
""")


driver.execute_script("$('select[name='condition'] option:eq(30)').prop('selected', true);")

More jquery links can be found at: https://code.jquery.com/

Jortega
  • 3,616
  • 1
  • 18
  • 21
  • Don't do that, do it [like this](https://stackoverflow.com/questions/57941221/how-can-i-use-jquery-with-selenium-execute-script-method). Otherwise you have timing issues. – pguardiario Jan 20 '20 at 13:29
  • @pguardiario what if you do not want to save the `.js` file locally? – Jortega Jan 20 '20 at 14:23
  • In that case do [like this](https://github.com/pguardiario/scrape_table/blob/master/scrape_table.py) – pguardiario Jan 20 '20 at 23:52
0
driver.execute_script("""
  $('select[name="condition"] option:eq(30)').prop('selected', true);
""")

You can also leave out the quotes in the attribute to be safer (since there are no spaces)

driver.execute_script("""
  $('select[name=condition] option:eq(30)').prop('selected', true);
""")
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • To the Einstein who downvoted this, I'm addressing the syntax error, which is the actual question, rather than injecting jQuery, which is not. – pguardiario Jan 20 '20 at 13:39