Selenium安装以及驱动浏览器建议步骤

1.Selenium 2.0是最新的版本,如果还有最新可以到官网上下载,使用pip工具安装即可

命令:pip install selenium

2.Selenium官网上拿的测试代码,如下,本来是FireFox的,被我改成Chrome,IE和FireFox也是可以的,毕竟Selenium都兼容

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Firefox driver
driver = webdriver.Chrome()

# go to the google home page
driver.get("http://www.baidu.com")

# the page is ajaxy so the title is originally this:
print driver.title

# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")

# type in the search
inputElement.send_keys("cheese!")

# submit the form (although google automatically searches now without submitting)
inputElement.submit()

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))

    # You should see "cheese! - Google Search"
    print driver.title

finally:
    driver.quit()

3.要驱动这段代码,还要安装浏览器对应版本的驱动,驱动网上有很多下载地址,百度一下即可,下载后,将驱动解压到浏览器安装目录,再将目录添加Path中,测试代码既可以(如果一开始没有装驱动,但是又打开了cmd,装好驱动后需要重启cmd才可以运行py文件,否则Path不生效)

 

4.补充不同chrome下载驱动的链接:http://chromedriver.storage.googleapis.com/index.html(链接来源用的源码路径:Python27\Lib\site-packages\selenium\webdriver\chrome\webdriver.py 其中的注释说明:

"""
    Controls the ChromeDriver and allows you to drive the browser.

    You will need to download the ChromeDriver executable from
    http://chromedriver.storage.googleapis.com/index.html
    """

5.Selenium python文档:https://selenium-python.readthedocs.io/installation.html

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值