Python爬虫框架 Mac安装selenium

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guohan_solft/article/details/85013197

一、终端安装

[localhost:~ guohan$ pip install selenium
Collecting selenium
  Downloading https://files.pythonhosted.org/packages/b8/53/9cafbb616d20c7624ff31bcabd82e5cc9823206267664e68aa8acdde4629/selenium-3.14.0-py2.py3-none-any.whl (898kB)
    100% |████████████████████████████████| 901kB 290kB/s 
Requirement already satisfied: urllib3 in /usr/local/lib/python3.6/site-packages (from selenium) (1.23)
Installing collected packages: selenium
Successfully installed selenium-3.14.0
localhost:~ guohan$ python3 
Python 3.6.2 (default, Jul 17 2017, 16:44:47) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> print(1)

显示安装成功

二、pycharm安装导入selenium包

打开一个python文件输入from selenium import webdriver发现报错,证明pycharm没有selenium包,需要手动导入

1、打开Pycharm --> Prereference --> Project:… --> Project Interpreter

查看到列表中没有selenium包

2、点击列表下左下侧的+号,弹出页面搜索selenium,然后点击右下侧install导入包

显示导入成功,不再报错。

3、也可以通过file --> DefaultSettings --> Project Interpeter来选择python的安装库来导入,此时,只要python包导入了也就是终端导入了,此处就可以直接用,相当于这是引用已导入的库

三、测试使用

1、新建python文件,注意名字不要是selenium.py,否则与库中的文件重名而报错

2、运行以下代码

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

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

# Record the search window
search_windows = driver.current_window_handle

# Deal with Baidu search
elem = driver.find_element_by_id("kw")
elem.clear()

elem.send_keys("pycon")
elem.send_keys("python selenium programmer")
search = driver.find_element_by_id("su")
search.click()

# Open a registration page
login = driver.find_element_by_name('tj_login')
login.click()
driver.find_element_by_link_text(u'立即注册').click()

# Get all window handles
all_handles = driver.window_handles

# If handle is not search, input some thing in registration page
for handle in all_handles:
    if handle != search_windows:
       driver.switch_to.window(handle)
       print('now register window!')
       driver.find_element_by_name("userName").send_keys('username')
       driver.find_element_by_name('phone').send_keys('password')
       time.sleep(2)

# If handle is search, switch to the search windows and do some search again
for handle in all_handles:
    if handle == search_windows:
       driver.switch_to.window(search_windows)
       print('now sreach window!')
       driver.find_element_by_id('TANGRAM__PSP_2__closeBtn').click()
       driver.find_element_by_id("kw").send_keys("selenium")
       driver.find_element_by_id("su").click()
       time.sleep(2)

driver.close()
  1. 报错:
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

解决方法:下载-解压-配置环境变量-测试
下载:选择适合自己系统的版本

介绍:https://remarkablemark.org/blog/2016/11/06/selenium-geckodriver/
下载:https://github.com/mozilla/geckodriver/releases

配置环境变量:将其配置在path的最前面,否则pycharm还是找不到,由于解压后的geckodriver就是可执行文件,因此配置环境变量时直接到上级目录即可。

cd ~
vim .bash_profile

测试如上代码,自动打开火狐浏览器,成功安装

猜你喜欢

转载自blog.csdn.net/guohan_solft/article/details/85013197