IE和Chrome无法使用Selenium2 Python(IE and Chrome not working with Selenium2 Python)
我似乎无法通过Selenium 2的Python库打开谷歌浏览器或Internet Explorer。 我使用的是Windows 7,64位。
我已完成以下步骤:
- 已安装的python - 2.7.5
- 安装硒2.33
- 在环境变量 - 路径中包含C:\ Python27&C:\ Python27 \ Scripts
- 下载32位(我正在运行64位但我找不到32位版本)windows Chrome驱动程序支持v27-30(我28岁)并将其放入C:\ Python27 \ Scripts
- 下载支持IE9的64位IE驱动程序(我将IE10降级为IE9)。 我将驱动程序放入C:\ Python27 \ Scripts
每当我打字:
from selenium import webdriver driver = webdriver.Ie()
要么
from selenium import webdriver driver = webdriver.Chrome()
进入Python shell,没有浏览器弹出,shell只会冻结几分钟然后输出错误信息。
IE错误消息:
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the IEDriver'
Chrome错误消息:
urllib2.HTTPError: HTTP Error 503: Service Unavailable
它与firefox完美搭配。 有趣的是,该过程(IEDriver和ChromeDriver)是根据TaskManager启动的,但窗口永远不会出现。
I can't seem to open up Google Chrome or Internet Explorer through Selenium 2's Python library. I am using Windows 7, 64 bit.
I have completed the following steps:
- Installed python - 2.7.5
- Installed selenium 2.33
- Included C:\Python27 & C:\Python27\Scripts in the Environment Variable - Path
- Downloaded the 32 bit (I am running 64 bit but I could not find the 32 bit version) windows Chrome Driver that supports v27-30 (I am on 28) and placed it into C:\Python27\Scripts
- Downloaded the 64 bit IE driver that supports up to IE9 (I downgraded IE10 to IE9). I placed the driver into C:\Python27\Scripts
Whenever I type:
from selenium import webdriver driver = webdriver.Ie()
OR
from selenium import webdriver driver = webdriver.Chrome()
into the Python shell, no browser pops up, the shell just freezes for a couple of minutes and then outputs an error message.
IE Error message:
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the IEDriver'
Chrome Error message:
urllib2.HTTPError: HTTP Error 503: Service Unavailable
It works perfectly fine with firefox. The funny thing is, is that the process (IEDriver and ChromeDriver) starts per the TaskManager, but the window never shows up.
最满意答案
在python-selenium
webdriver.Ie
只是执行IEDriver.exe并通过webdriver.Remote
连接到它的快捷方式。 例如,您可以从命令行启动IEDriver.exe :> IEDriverServer.exe Started InternetExplorerDriver server (64-bit) 2.39.0.0 Listening on port 5555
并使用以下代码替换
webdriver.Ie()
:webdriver.Remote(command_executor='http://127.0.0.1:5555', desired_capabilities=DesiredCapabilities.INTERNETEXPLORER)`
你会得到相同的结果。
特别是在您的情况下,您很可能有一些系统代理设置强制它通过代理服务器连接到127.0.0.1 。 可能当您按照回答Python中的描述禁用它时:在urllib2中禁用http_proxy ,您可以解决问题:
import selenium import urllib2 from contextlib import contextmanager @contextmanager def no_proxies(): orig_getproxies = urllib2.getproxies urllib2.getproxies = lambda: {} yield urllib2.getproxies = orig_getproxies with no_proxies(): driver = selenium.webdriver.Ie() driver.get("http://google.com")
In python-selenium
webdriver.Ie
is just a shortcut for executing IEDriver.exe and connecting to it throughwebdriver.Remote
. For example, you could start IEDriver.exe from command line:> IEDriverServer.exe Started InternetExplorerDriver server (64-bit) 2.39.0.0 Listening on port 5555
and replace
webdriver.Ie()
with the following code:webdriver.Remote(command_executor='http://127.0.0.1:5555', desired_capabilities=DesiredCapabilities.INTERNETEXPLORER)`
You will get the same result.
Specifically in your case is most likely that you have some system proxy settings that force it connect to 127.0.0.1 through proxy server. Probably when you disable it as described in answer Python: Disable http_proxy in urllib2, you can solve the problem:
import selenium import urllib2 from contextlib import contextmanager @contextmanager def no_proxies(): orig_getproxies = urllib2.getproxies urllib2.getproxies = lambda: {} yield urllib2.getproxies = orig_getproxies with no_proxies(): driver = selenium.webdriver.Ie() driver.get("http://google.com")
相关问答
更多-
selenium2执行get()报unbound method get() must be called with WebDriver instance as first argument[2023-04-03]
testdriver = webdriver.Firefox() 不能少了括号 -
如何在Chrome Selenium Python中禁用Java脚本(How to disable java script in Chrome Driver Selenium Python)[2023-01-24]
这真的很困难。 你可以尝试这样做: DesiredCapabilities caps = DesiredCapabilities.chrome(); caps.setCapability("chrome.switches", Arrays.asList("--disable-javascript")); 但正如它在这里写的,如果你使用ChromeDriver2,你不能禁用JavaScript。 It's really difficult. You can try doing this way: Desir ... -
使用无代理Chrome通过Python为Selenium禁用代理(Disable proxy via Python for Selenium with headless Chrome)[2022-10-18]
尝试这个: chrome_options.add_argument('--no-proxy-server') Try this: chrome_options.add_argument('--no-proxy-server') -
您需要指定您的chrome驱动程序所在的路径 。 从此处下载所需平台的Chrome驱动器 。 将该文件放置在您的代码所在的目录或无论您想要的位置。 通过以下代码链接您的chromedriver.exe : browser = webdriver.Chrome(executable_path=r"chromedriver.exe") 或将executable_path更改为存储您的chromedriver的位置。 你完成了! 这应该做到这一点。 希望能帮助到你! 评论你是否还有任何疑问。 You need ...
-
python selenium chrome webdriver给我数据;(python selenium chrome webdriver giving me data; page)[2023-01-15]
最可能的是您的浏览器与webdriver不兼容。 请确认您的浏览器版本和webdriver版本。 我的chrome版本是60,chromerdriver是chromedriver_2.30.exe The most possibility is your browser not compatible with the webdriver. Please confirm your browser version and webdriver version. My chrome version is 60 an ... -
在python-selenium webdriver.Ie只是执行IEDriver.exe并通过webdriver.Remote连接到它的快捷方式。 例如,您可以从命令行启动IEDriver.exe : > IEDriverServer.exe Started InternetExplorerDriver server (64-bit) 2.39.0.0 Listening on port 5555 并使用以下代码替换webdriver.Ie() : webdriver.Remote(command_ex ...
-
Python3.5 Selenium2 Webdriver如何查看脚本链接?(Python3.5 Selenium2 Webdriver how to look to scripts links?)[2022-10-18]
您看到的内容是WebElement类实例的字符串表示形式 。 如果要查看src属性值,请调用get_attribute() : print(find_js.get_attribute("src")) 如果要查看元素的文本: print(find_js.text) What you see printed is the string representation of a WebElement class instance. If you want to see the src attribute val ... -
尝试使用以下代码,如有任何问题,请告知我们: from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("test-type") driver = webdriver.Chrome(chrome_options=chrome_options) driver.get('https://www.google.com') Try to use following c ...
-
我终于找到了一个工作: 我贴了 "print.always_print_silent": "true", "print.show_print_progress": "false", 进入Firefox用户配置文件。 Selenium拥有自己的默认用户配置文件,您可以编辑它。 包含配置文件的文件称为firefox_profile.py 请注意,这只能在不通过弹出窗口询问用户的情况下进行打印。 它适用于我,因为我没有连接打印机。 我在r-kiosk插件的介绍中发现了这些代码行。 I final ...
-
更可能的是,这是因为python引用了错误的路径,通常是路径中~/的主目录快捷方式。 Python将尝试从当前目录运行该文件,例如,如果您的代码在~/Dev/testproject ,并且上面调用的代码实际上是在尝试运行/home/username/Dev/testproject/~/Library/Application Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx 尝试使用以下内容: ...