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);")
5
  • @AhmadIshaq This sounds like an X-Y problem. 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? Commented Jan 15, 2020 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 code.jquery.com/jquery-3.4.1.js Commented Jan 15, 2020 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 "$" Commented Jan 15, 2020 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: Commented Jan 16, 2020 at 7:13
  • <label class="json-form-item select condition std variant-select"> <div class="label-wrapper"> <span class="label">condition</span> </div> <select tabindex="1" name="condition" class="json-form-input no-js condition"> <option value="" selected >-</option> <option value="10" >new</option> <option value="20" >like new</option> <option value="30">excellent</option> <option value="40" >good</option> <option value="50" >fair</option> <option value="60" >salvage</option> </select> </label> Commented Jan 16, 2020 at 7:13

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

3 Comments

Don't do that, do it like this. Otherwise you have timing issues.
@pguardiario what if you do not want to save the .js file locally?
In that case do like this
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);
""")

1 Comment

To the Einstein who downvoted this, I'm addressing the syntax error, which is the actual question, rather than injecting jQuery, which is not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.