您的位置:首页 > 编程语言 > Python开发

python+selenium 编写第一个自动化脚本 Ie版

2017-11-15 10:09 411 查看
# coding=utf-8
from selenium import webdriver

driver = webdriver.Ie()
driver.get("http://www.baidu.com")

driver.find_element_by_id("kw").send_keys("Selenium2")
driver.find_element_by_id("su").click()
driver.quit()

 敲入如上代码,将代码保存为baidu.py(注意Ie大小写)

下载ie浏览器驱动,下载后将驱动保存在与python.exe同一个目录下,

记得将python路径和pip路径放到path中

右击用python idle打开,按F5运行,运行报错,报错如下:

Traceback (most recent call last):

  File "C:\Users\lish\Desktop\baidu.py", line 4, in <module>

    driver = webdriver.Ie()

  File "C:\Users\lish\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\ie\webdriver.py", line 82, in __init__

    desired_capabilities=capabilities)

  File "C:\Users\lish\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 151, in __init__

    self.start_session(desired_capabilities, browser_profile)

  File "C:\Users\lish\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 240, in start_session

    response = self.execute(Command.NEW_SESSION, parameters)

  File "C:\Users\lish\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute

    self.error_handler.check_response(response)

  File "C:\Users\lish\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

解决方法:把安全下红色框内的四个选项都勾上,然后保存,重启浏览器,再次回到idle,按F5运行即可




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python selenium