Selenium Webdriver:execute_script 无法执行自定义方法和外部 javascript 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23228247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Webdriver: execute_script can't execute custom methods and external javascript files
提问by estemendoza
I'm working with Selenium and Python and I'm trying to do two things:
我正在使用 Selenium 和 Python,我正在尝试做两件事:
- Import an external javascript file and execute a method defined there
- Define methods on a string and call them after evaluating
- 导入外部 javascript 文件并执行在那里定义的方法
- 在字符串上定义方法并在评估后调用它们
This is the output for the first case:
这是第一种情况的输出:
test.js
测试.js
function hello(){
document.body.innerHTML = "testing";
}
Python code
Python代码
>>> from selenium import webdriver
>>> f = webdriver.Firefox()
>>> f.execute_script("var s=document.createElement('script');\
... s.src='file://C:/test.js';\
... s.type = 'text/javascript';\
... document.head.appendChild(s)")
>>> f.execute_script("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\selenium-2.41.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 394, in execute_script
{'script': script, 'args':converted_args})['value']
File "C:\Python27\lib\site-packages\selenium-2.41.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 166, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.41.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: u'hello is not defined' ; Stacktrace:
at anonymous (about:blank:68)
at handleEvaluateEvent (about:blank:68)
And for the second case:
对于第二种情况:
>>> js = "function blah(){document.body.innerHTML='testing';}"
>>> f.execute_script(js)
>>> f.execute_script("blah")
...
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: u'blah is not defined' ; Stacktrace:
回答by Louis
I can get your first case to work if I create an empty html file and issue:
如果我创建一个空的 html 文件并发出:
f = webdriver.Firefox()
f.get("file://path/to/empty.html")
After this, the JavaScript you've shown will execute without issue. When I try the code you've shown in the question, Firefox does not give me an error but Chrome says: "Not allowed to load local resource". I believe the problem is cross-domain requests.
在此之后,您显示的 JavaScript 将毫无问题地执行。当我尝试你在问题中显示的代码时,Firefox 没有给我一个错误,但 Chrome 说:“不允许加载本地资源”。我相信问题是跨域请求。
The issue with your second case is that behind the scenes Selenium wraps your JavaScript code in an anonymous function. So your blah
function is local to this anonymous function. If you want to make it global, you have to assign it to window
, like this:
您的第二种情况的问题在于,Selenium 在幕后将您的 JavaScript 代码包装在一个匿名函数中。所以你的blah
函数是这个匿名函数的本地函数。如果你想让它成为全球性的,你必须将它分配给window
,就像这样:
>>> from selenium import webdriver
>>> f = webdriver.Firefox()
>>> f.execute_script("window.blah = function () {document.body.innerHTML='testing';}")
>>> f.execute_script("blah()")
回答by estemendoza
driver.execute_script("window.a = function(a,b) {return a + b;}")
print driver.execute_script("return a(1,2)")