为什么要用 Anaconda?
Anaconda(官方网站)就是可以便捷获取包且对包能够进行管理,同时对环境可以统一管理的发行版本。
在wind上直接搭建环境很麻烦,容易出错。Anaconda简单安装。
第一步 安装 Anaconda
Anaconda在官网就可以下载,网址:https://www.anaconda.com/download/ ,官网上可以选择各个操作系统的安装包。然后运行exe文件安装。 注意
:记住安装路径
注意
:这里需要勾上,添加到path环境中,如果没有沟,后面需要手动添加到path中。

安装完成后如下图。

第二步 安装虚拟环境
1.打开Anaconda Powershell Prompt终端

2.创建虚拟环境
查看conda中虚拟环境
conda inffo -e
创建新的虚拟环境
conda create -n 新的环境名 python=指定版本(2.7/3.6/3.7)
# 注意需要在base中运行,不能在虚拟环境中执行
激活虚拟环境
conda activate 新的环境名


第三步 安装selenium
进入虚拟环境后执行
pip install selenium
# 如果需要其它包也用pip install安装,同Linux命令。

第四步 chromedriver下载
版本对应:
- 如果您使用的是Chrome版本77,请下载 ChromeDriver 77.0.3865.40
- 如果您使用的是Chrome版本76,请下载 ChromeDriver 76.0.3809.126
- 如果您使用的是Chrome版本75,请下载 ChromeDriver 75.0.3770.140
第五步 chromedriver添加到path环境中。
查询python的path环境,进入ipython
。 然后
import sys
sys.path

当然,也可以放到项目路径下。
第六步 代码。
class ShellChrome(object):
def __init__(self):
self.phone_port = PhonePort()
self.shell_mongo = ShellMongo()
mobile_emulation = {"deviceName": "Nexus 5"}
self.option = webdriver.ChromeOptions()
self.option.add_experimental_option("mobileEmulation", mobile_emulation) # 转换手机模式,不需要注射。
self.option.add_argument('disable-infobars')
self.option.add_argument('user-agent=' + ua) # 添加ua
self.driver = webdriver.Chrome(chrome_options=self.option, executable_path='chromedriver.exe') # 特别注意,wind下要带.exe
self.driver.maximize_window()
def login(self):
print("正在打开注册页面...")
# 让浏览器不要显示当前受自动化测试工具控制的提醒
self.driver.get("https://xxxxxxxx") #
# 等待元素显示在页面
WebDriverWait(self.driver, 15).until(EC.visibility_of_element_located((By.NAME, 'tel')))
# 获取验证码
self.driver.find_element_by_xpath('//span[1]').click()
# 输入验证码
tel = self.driver.find_element_by_xpath('//input')
tel.send_keys(validate_code)
print("正在关闭注册页面...")
self.driver.quit()
下一篇,使用PyCharm连接Anaconda,调用chromedriver。
网友评论