在Selenium Python中执行Jquery?



我有运行这个的问题。我收到一个错误,指出我有语法问题。我需要语法方面的帮助。

driver.execute_script('$('select[name='condition'] option:eq(30('(.prop('selected', true(;'( ^ 语法错误:语法无效

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

因此,如果 jquery 不在页面上,您需要先添加它。我只是将其附加到下面的页面。然后运行代码。

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

更多jquery链接可以在以下位置找到:https://code.jquery.com/

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

您也可以省略属性中的引号以更安全(因为没有空格(

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

最新更新