最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中搭建Selenium的Python开发环境

Mac crifan 1639浏览 0评论
折腾:
【未解决】Mac中用Selenium自动操作浏览器实现百度搜索
期间,去Mac中搭建Selenium的Python开发环境。
Mac Selenium
selenium python
mac 搭建selenium与ChromeDriver环境 – 简书
直接用pip安装selenium:
 pip3 install selenium       
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting selenium
  Downloading http://mirrors.aliyun.com/pypi/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904 kB)
     |████████████████████████████████| 904 kB 1.2 MB/s
Requirement already satisfied: urllib3 in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from selenium) (1.25.8)
Installing collected packages: selenium
Successfully installed selenium-3.141.0
WARNING: You are using pip version 20.2.2; however, version 21.0.1 is available.
You should consider upgrading via the '/Users/crifan/.pyenv/versions/3.6.6/bin/python3.6 -m pip install --upgrade pip' command.
顺带去:
 pip install --upgrade pip   
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pip
  Downloading http://mirrors.aliyun.com/pypi/packages/fe/ef/60d7ba03b5c442309ef42e7d69959f73aacccd0d86008362a681c4698e83/pip-21.0.1-py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 4.0 MB/s 
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.2.2
    Uninstalling pip-20.2.2:
      Successfully uninstalled pip-20.2.2
Successfully installed pip-21.0.1
看看官网,是不是要下载webdriver:
【已解决】Mac中下载Selenium的Chrome的driver:ChromeDriver
Mac os上配置selenium并使用python操作web页面_iluxiaoxiaoniao的博客-CSDN博客_mac python webdriver
然后继续,写测试代码:
# Function: demo selenium do baidu search
# Author: Crifan Li
# Update: 20210318

from selenium import webdriver

baiduUrl = "https://www.baidu.com"
chromeDriver = webdriver.Chrome()
chromeDriver.get(baiduUrl)
print("title=%s" % chromeDriver.title)
效果是可以的:
可以控制Chrome浏览器,打开百度首页,得到title的。
至此,Selenium的开发环境就搭建好了。
selenium · PyPI
【总结】
Mac中搭建Selenium的逻辑是:
  • Python中安装Selenium库
    • pip install selenium
  • Mac中下载某个webdriver
    • 此处选用Chrome的driver:ChromeDriver
      • 需要下载到二进制的chromedriver,并确保PATH中能找到
        • 详见:【已解决】Mac中下载Selenium的Chrome的driver:ChromeDriver
然后用代码:
from selenium import webdriver

baiduUrl = "https://www.baidu.com"
chromeDriver = webdriver.Chrome()
chromeDriver.get(baiduUrl)
print("title=%s" % chromeDriver.title)
即可打开Chrome浏览器,且能看到Chrome中提示:Chrome正在受到自动测试软件的控制。
能继续打开百度首页,获取到title内容。

转载请注明:在路上 » 【已解决】Mac中搭建Selenium的Python开发环境

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.199 seconds, using 22.14MB memory