简体   繁体   English

无法使用 python 在 selenium webdriver 中返回 javascript 确认值

[英]Unable to return javascript confirm value in selenium webdriver using python

in my code i am giving a javascript confirm to ask the user to click on oK or cancel.在我的代码中,我给出了 javascript 确认以要求用户单击确定或取消。 iF the user is clicking on OK then a certain set of code will be executed.如果用户单击确定,那么将执行一组特定的代码。 i am using python and selenium webdriver but everytime i run the below code null value is being printed even after clicking OK or cancel我正在使用 python 和 selenium webdriver,但每次我运行以下代码 null 值时,即使在单击确定或取消后仍在打印

ss=driver.execute_script(" fg = confirm('Press a button;'); return fg; ") ss=driver.execute_script(" fg = confirm('Press a button;'); return fg; ")

print(ss)打印(ss)

time.sleep(5)时间.sleep(5)

What's up?这是怎么回事?

So, in JavaScript you can't set a variable without a var , let or const in code.因此,在 JavaScript 中,您不能在代码中设置没有varletconst的变量。

And, you can't return nothing if ins't a function.而且,如果不是 function,您将无法返回任何内容。

The code, will be:代码将是:

ss=driver.execute_script("() => const fg = confirm('Press a button!'); return fg; ")

print(ss)

time.sleep(5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM