Ahmad Ishaq
Ahmad Ishaq

Reputation: 21

execute Jquery in selenium Python?

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);")

Upvotes: 1

Views: 7209

Answers (2)

Jortega
Jortega

Reputation: 3790

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/

Upvotes: 2

pguardiario
pguardiario

Reputation: 55002

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);
""")

Upvotes: 0

Related Questions