[英]Webdriver in Python - Executing External JavaScript
I like to execute a JavaScript in webdriver in python. 我喜欢在python中的webdriver中执行JavaScript。 Unfortunately they way I try to implement it does not work.
不幸的是,他们试图实现它的方式我行不通。 How can I correctly do it?
我该怎么做呢?
The respective documentary states: ( http://selenium-python.readthedocs.org/en/latest/api.html ) 相应的文档状态:( http://selenium-python.readthedocs.org/en/latest/api.html )
driver.execute_script(‘document.title’)
So I wrote the following python code: 所以我写了下面的python代码:
driver = webdriver.Firefox()
driver.get("http://google.com")
driver.execute_script("./hello_world.js")
driver.quit()
With the respective hello_world.js within the same directory: 将各自的hello_world.js放在同一目录中:
alert('Hello, World!')
Yet, unfortunately it yields a Message syntax error: 但是,不幸的是,它会产生Message语法错误:
Log: 日志:
Traceback (most recent call last):
File "/sinonJS_test.py", line 44, in <module>
sinon_test()
File "/sinonJS_test.py", line 35, in sinon_test
driver.execute_script("./hello_world.js")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 401, in execute_script
{'script': script, 'args':converted_args})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: syntax error
Stacktrace:
at handleEvaluateEvent (http://google.com:68:11)
Solution attemps: 1) Tried to permute hello_world.js file path description, like adding/removing file suffix, adding/removing absolute file path. 解决方案尝试:1)试图置换hello_world.js文件路径说明,例如添加/删除文件后缀,添加/删除绝对文件路径。 Not working.
不工作
Note: Indeed I researched several answered threads to similar questions here on SO, yet none of them appeared to solve my problem. 注意:的确,我在SO上研究了类似问题的多个回答线程,但似乎没有一个可以解决我的问题。 Eg some involving only very small scripts solved the issue by stating the JavaScript as a string within the actual python code.
例如,一些只涉及非常小的脚本的脚本通过将JavaScript声明为实际python代码中的字符串来解决了该问题。 This is not a not an option to me, as I nee to execute bigger more complex JavaScripts (Sinon Fake Timers).
这不是我的选择,因为我需要执行更大,更复杂的JavaScript(Sinon Fake Timers)。
Like this one: Selenium Webdriver: execute_script can't execute custom methods and external javascript files 像这样: Selenium Webdriver:execute_script无法执行自定义方法和外部javascript文件
You need to give a string containing javascript as argument to driver.execute_script. 您需要提供一个包含javascript的字符串作为driver.execute_script的参数。 In your case if you want to execute a script written inside a file, just read the file and execute that.
在您的情况下,如果要执行写在文件中的脚本,只需读取文件并执行即可。 Like this
像这样
driver.execute_script(open("./hello_world.js").read())
with appropriate location of hello_world.js 具有hello_world.js的适当位置
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.