如何通过Python在Selenium中禁用网页安全?

1 投票
1 回答
3013 浏览
提问于 2025-04-16 16:50

显然,google-chrome遇到这个问题是很常见的:http://jira.openqa.org/browse/SRC-740

关键是要在不启用安全模式的情况下启动它。要禁用安全模式,

"--disable-web-security",

不过我在想怎么才能正确地指定这些命令行参数,所以在这里的open调用时出现了问题:

from selenium import selenium

sel = selenium('localhost', 4444, '*googlechrome', 'http://www.google.com/')
sel.start()
sel.open('/')

这是我启动selenium服务器的方法:

shogun@box:~$ java -jar selenium-server-standalone-2.0b3.jar

1 个回答

0

为了让这个工作正常,我需要创建一个外部脚本来包裹Chrome浏览器。你可以把这个脚本放在Selenium服务器可以访问的地方(我把它放在~/bin/startchrome,并设置为可执行):

#!/bin/sh
# chrome expects to be run from the .app dir, so cd into it
# (the spaces in the path are a Mac thing)
cd /Applications/Google\ Chrome.app
exec ./Contents/MacOS/Google\ Chrome --disable-security $*

然后在你的Python代码中,做这个:

from selenium import selenium

browser = '*googlechrome /Users/pat/bin/startchrome'
sel = selenium('localhost', 4444, browser, 'http://www.google.com')
sel.start()
sel.open('/')

撰写回答