Javascript 自动化 Chrome
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4730906/
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
Automating Chrome
提问by Volomike
I've seen some wild things happen when I paste some urlencoded Javascript into a URL on Firefox and Chrome. Is it possible to use this technique to tell Chrome to visit a URL and then save it as a file? I'm trying to automate Chrome, and Selenium looked extremely daunting.
当我将一些 urlencoded Javascript 粘贴到 Firefox 和 Chrome 上的 URL 中时,我看到了一些疯狂的事情。是否可以使用这种技术告诉 Chrome 访问 URL,然后将其保存为文件?我正在尝试自动化 Chrome,而 Selenium 看起来非常令人生畏。
EDIT: Unfortunately, I forgot to be more clear here. Let me explain. Things like wget, curl, etc. won't work because I have to get through logins in some of these scripts. And I've looked at iMacros, but found that I can't get them to run from command line except on Windows, unless I pay for the $499 package. Some other advantages of GCEs are that the dev platform is free and open to some degree, and they are cross-platform. (I use Linux.)
编辑:不幸的是,我忘了在这里更清楚。让我解释。wget、curl 等将无法工作,因为我必须在其中一些脚本中登录。我看过 iMacros,但发现我无法让它们从命令行运行,除非在 Windows 上运行,除非我支付 499 美元的包。GCE 的其他一些优点是开发平台在某种程度上是免费和开放的,并且它们是跨平台的。(我使用 Linux。)
EDIT: At this point, I'm learning about Google Chrome Extensions. It appears they are easy to build and will let me (I think) tell the browser to open a new tab, go to a page, manipulate the DOM on that page (such as populating some fields and logging in), and then manipulate the DOM on the response page. GCEs don't let you do File I/O, so they are not like Firefox Extensions with XPCOM, but you can get around that by using AJAX to send data to a backend script (like a PHP script on a LAMP server) to save that data.
编辑:此时,我正在学习 Google Chrome 扩展。看起来它们很容易构建,并且会让我(我认为)告诉浏览器打开一个新选项卡,转到一个页面,操作该页面上的 DOM(例如填充一些字段并登录),然后操作响应页面上的 DOM。GCE 不允许您进行文件 I/O,因此它们不像带有 XPCOM 的 Firefox 扩展,但是您可以通过使用 AJAX 将数据发送到后端脚本(如 LAMP 服务器上的 PHP 脚本)以保存那个数据。
EDIT: By the way, and this is slightly off-topic (but I add to clarify) those "wild things" in Javascript that I mentioned were when you manipulate the DOM creating a URL that looks like so:
编辑:顺便说一句,这有点偏离主题(但我补充说明)我提到的 Javascript 中的那些“疯狂的东西”是当你操作 DOM 创建一个看起来像这样的 URL 时:
javascript:(function(){...your URL-encoded Javascript here...})();
采纳答案by Volomike
The solution appears to be to make one's own Google Chrome Extension (GCE). It is easy to learn within about 4 hours if you know how to do slightly advanced Javascript stuff, and is very powerful. I can use the Tabs API to create a new tab and go to a specific URL. I can then inject jQuery into that URL and make it manipulate the DOM or do anything we normally can do with jQuery. I can't do file I/O, but there are two workarounds. One, I can force the browser to download a file from a remote location, and I can send data from the current page back up to a remote server via jQuery's $.get() or $.post() calls.
解决方案似乎是制作自己的Google Chrome 扩展程序 (GCE)。如果你知道如何做一些稍微高级的 Javascript 东西,它在大约 4 小时内很容易学习,并且非常强大。我可以使用 Tabs API 创建一个新选项卡并转到特定 URL。然后,我可以将 jQuery 注入到该 URL 中并使其操作 DOM 或执行我们通常可以使用 jQuery 执行的任何操作。我无法进行文件 I/O,但有两种解决方法。一,我可以强制浏览器从远程位置下载文件,我可以通过 jQuery 的 $.get() 或 $.post() 调用将数据从当前页面发送回远程服务器。
回答by Thai
You can use Python to automate web tasks using pywebkitgtk. It is a Python binding for WebKitGtk, which uses the WebKit engine, the same engine as chrome.
您可以使用 Python 使用pywebkitgtk自动执行 Web 任务。它是 WebKitGtk 的 Python 绑定,它使用 WebKit 引擎,与 chrome 引擎相同。
Thanks to this blog post, pywebkitgtk - Execute JavaScript from Python, I made a subclass of webkit.WebView
to make these tasks easier.
感谢这篇博文pywebkitgtk - 从 Python 执行 JavaScript,我创建了一个子类webkit.WebView
来简化这些任务。
import gtk
import webkit
import json
class WebView(webkit.WebView):
def eval_script(self, script):
self.execute_script('oldtitle=document.title;document.title="!!!!";document.title=JSON.stringify(eval(' + json.dumps(script) + '));')
result = json.loads(self.get_main_frame().get_title())
self.execute_script('document.title=oldtitle;')
return result
def wait_for_load(self):
handle = None
def load_status_cb(view, frame):
if frame == view.get_main_frame():
self.disconnect(handle)
gtk.main_quit()
handle = self.connect('load-finished', load_status_cb)
gtk.main()
I added the function called eval_script
which is like execute_script
, but you could get the results of the function as Python objects. You just need to make sure that what you are evaluating is JSON-serializable.
我添加了名为eval_script
like的函数execute_script
,但您可以将函数的结果作为 Python 对象获取。您只需要确保您正在评估的内容是 JSON 可序列化的。
Also, I added a wait_for_load
function which is pretty self-explanatory.
另外,我添加了一个wait_for_load
非常不言自明的函数。
To set up a UI, you first have to create a window, a scrolled window, and a web view.
要设置 UI,您首先必须创建一个窗口、一个滚动窗口和一个 Web 视图。
# window
window = gtk.Window()
window.set_default_size(800, 600)
# scroll view
scroll_view = gtk.ScrolledWindow()
scroll_view.props.hscrollbar_policy = gtk.POLICY_AUTOMATIC
scroll_view.props.vscrollbar_policy = gtk.POLICY_AUTOMATIC
# web view
web_view = WebView()
# events
window.connect('delete-event', lambda window, event: gtk.main_quit())
# show
scroll_view.add(web_view)
window.add(scroll_view)
window.show_all()
Then you can start automating things! For example, this code loads StackOverflow's login page, click the Facebook login button, fills in the username and password (in this case "test"). Finally, it shows the login button text.
然后你就可以开始自动化了!例如,此代码加载 StackOverflow 的登录页面,单击 Facebook 登录按钮,填写用户名和密码(在本例中为“test”)。最后,它显示登录按钮文本。
# the script is here
web_view.open('http://www.stackoverflow.com/users/login')
web_view.wait_for_load()
web_view.execute_script('openid.signin("facebook")')
web_view.wait_for_load()
web_view.execute_script('document.querySelector("#email").value = "test"')
web_view.execute_script('document.querySelector("#pass").value = "test"')
print "Login's button text is:", web_view.eval_script('document.querySelector("#buttons input[type=\"submit\"]").value')
In my case, the Facebook's interface was in Thai language, and I could see the login's button text.
就我而言,Facebook 的界面是泰语,我可以看到登录的按钮文本。
Login's button text is: ???????????
登录的按钮文字是:?????????????
You can also have it actually click the submit button, just by calling click()
on that element. (Note: click()
works for button elements, not on links)
您也可以让它实际单击提交按钮,只需调用click()
该元素即可。(注意:click()
适用于按钮元素,不适用于链接)
web_view.execute_script('document.querySelector("#buttons input[type=\"submit\"]").click()')
web_view.wait_for_load()
You will notice that after all the scripts are finished, the application closes itself without waiting.
您会注意到,在所有脚本完成后,应用程序无需等待就自行关闭。
If you want to keep the application running after it finish all the scripts in there, you need to add the last line:
如果要在应用程序完成其中的所有脚本后继续运行,则需要添加最后一行:
gtk.main()
Also, if you remove the window.show_all()
line and the last gtk.main()
line. Then your app will work without a GUI. (Note: You still need a display server.)
此外,如果您删除该window.show_all()
行和最后gtk.main()
一行。然后您的应用程序将在没有 GUI 的情况下运行。(注意:您仍然需要一个显示服务器。)
Right now, we don't have good pywebkitgtk docs yet, so you have to look at WebKitGtk's documentationinstead. Good luck.
现在,我们还没有好的 pywebkitgtk 文档,所以你必须查看WebKitGtk 的文档。祝你好运。
回答by Nick
Definitely check out Watir! I find it extremely straightforward. It works just as easily with Selenium as it does with Chrome, IE, or Firefox. Though the version for Chrome is not yet officially supported, they claim it is usable. I, myself have only used it for Selenium, IE, and Firefox.
一定要看看Watir!我觉得它非常简单。它在 Selenium 上的工作就像在 Chrome、IE 或 Firefox 上一样容易。虽然 Chrome 的版本尚未得到官方支持,但他们声称它可以使用。我自己只将它用于 Selenium、IE 和 Firefox。
Also, Watir easily integrates with Cucumber, if you're looking for cutting-edge BDD (Behavior-Driven Development). It's just Ruby code, is open source, and hosted on gitHub. Enjoy!
此外,如果您正在寻找尖端的 BDD(行为驱动开发),Watir 可以轻松地与Cucumber集成。它只是 Ruby 代码,是开源的,并托管在gitHub 上。享受!
回答by Angel Tsvetkov
You can checkout http://qaagent.com. This is an easy way to automate some web related task
您可以查看http://qaagent.com。这是自动化一些与网络相关的任务的简单方法
回答by kzh
You could try iMacros for Chrome. It is a pretty easy to use automation system.
你可以试试iMacros for Chrome。这是一个非常易于使用的自动化系统。
- Open iMacros
- Click Record.
- Go about you browsing routine.
- Click stop.
- 打开 iMacros
- 单击记录。
- 继续浏览常规。
- 单击停止。
I don't think it can get any easier than that. The scripts it saves are in plain text so you can edit them for some fine grain control if need be.
我认为没有比这更容易的了。它保存的脚本是纯文本格式,因此您可以在需要时对其进行编辑以进行一些细粒度控制。