python自动化测试-Selenium安装与使用

本文详细介绍了如何在Windows环境下安装Python和Selenium库,并提供了Selenium与各浏览器驱动的下载链接。通过展示安装过程及测试执行示例,确保了Selenium能成功驱动Chrome浏览器。此外,还展示了Selenium的基本使用方法,如打开百度并输入搜索关键字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 前提条件-先安装好python
    D:\now>python
    Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

     

  • 安装
    D:\now>pip install selenium
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
    Collecting selenium
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904 kB)
         |████████████████████████████████| 904 kB 24 kB/s
    Collecting urllib3
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0c/cd/1e2ec680ec7b09846dc6e605f5a7709dfb9d7128e51a026e7154e18a234e/urllib3-1.26.5-py2.py3-none-any.whl (138 kB)
         |████████████████████████████████| 138 kB 242 kB/s
    Installing collected packages: urllib3, selenium
    Successfully installed selenium-3.141.0 urllib3-1.26.5

     

  • 安装驱动

  1. 检查浏览器版本
  2. 各浏览器与驱动下载地址

    驱动chromedriver:http://chromedriver.storage.googleapis.com/index.html

    驱动geckodriver:https://github.com/mozilla/geckodriver/releases

    驱动IEDriverServer:http://selenium-release.storage.googleapis.com/index.html

    浏览器google:https://www.google.com/chrome/

    浏览器firefox:http://ftp.mozilla.org/pub/firefox/releases/

  3. 下载驱动
  4. 放到python根目录
  5.  
  • 测试  执行 from selenium import webdriver webdriver.Chrome() 自动启动谷歌浏览器证明安装成功

    • D:\now>python
      Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
      Type "help", "copyright", "credits" or "license" for more information.
      
      >>> from selenium import webdriver
      >>> webdriver.Chrome()
      
      DevTools listening on ws://127.0.0.1:54991/devtools/browser/0c068cc8-d4b1-4fe6-88a8-ecc4cfc21c00
      [12624:2956:0606/103032.352:ERROR:device_event_log_impl.cc(214)] [10:30:32.352] USB: usb_device_handle_win.cc:1058 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
      [12624:2956:0606/103032.371:ERROR:device_event_log_impl.cc(214)] [10:30:32.370] Bluetooth: bluetooth_adapter_winrt.cc:1162 RequestRadioAccessAsync failed: RadioAccessStatus::DeniedByUserWill not be able to change radio power.
      <selenium.webdriver.chrome.webdriver.WebDriver (session="df68c833ea3780c9380ededbf4a37566")>
      >>>

       

  • 使用

    • #!/usr/bin/python3
      from selenium import webdriver
      driver = webdriver.Chrome()
      driver.get("http://www.baidu.com")
      
      # Selenium 的定位元素
      # driver.find_element_by_id("su").send_keys("python教程")
      driver.find_element_by_xpath('//*[@id="su"]').send_keys("python教程")

       

  •  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值