初次使用Python+unitest+selenium环境配置

本文详细介绍了如何在Mac和Windows上下载和管理Python3、安装pip、创建和使用虚拟环境、以及使用Selenium进行Web自动化测试,包括解决Chrome版本兼容性和路径设置问题。
摘要由CSDN通过智能技术生成

一、下载python
1.mac自带python3,终端使用python3 --version即可查看版本
2.windows 下载安装python,一定要勾选自动添加到path选项!!cmd使用python3 --version即可查看版本

二、下载库管理工具pip
1.Mac:mac的python版本不是自带pip的所以需要在终端下载。正常情况下,输入代码:sudo easy_install pip 在Mac中是没办法下载的。因为现在python的版本是3.0,sudo easy_install是自带引导指向python2.7。所以就需要修改一下代码:pip3 install scrap,这样就指向现在python3的版本。因为pip3是咱们现在3.0版本自带的。就这样等几分钟下载好pip3
2.Windows:
2.1、通过python自带的pip安装器安装。在命令行中输入“python -m ensurepip --upgrade”命令即可安装最新版本的pip;
2.2、使用windows10的“windows powershell”安装pip;
2.3、从官网下载安装包解压后找到python.exe文件打开,输入python -m ensurepip --upgrade命令即可。

三、给pip添加指定下载源
1. Mac:在usr/目录下新建pip/pip.conf 文件,然后在下载源里搜索PYPI 库,输入登录密码(下载源的用户登录密码),选择pip-virtual 即可获取下载源密钥,将密钥复制到pip.conf, 即可,如果使用pip下载东西失败,根据失败把报错的域名,添加到pip.conf 末尾为信任trust=xxx.com
2. Windows: 同mac操作一遍

四、启动pyhton项目,新建虚拟环境enve
1.Mac/windows 在项目在执行:python -m enve <enveName>,即在根目录创建了一个虚拟环境,此时终端会显示当前位置是enve环境
2.激活项目的虚拟环境:
Mac: source ~/<enveName>/bin/activate
Windows: <enveName>/script/activate

五、创建py执行文件如:test_test1.py
1.在终端安装selenium: pip install selenium
2.在终端安装webview: XXXX
3.示例:

import unittest
from selenium import webdriver

class TestGoogleSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()  # 使用Chrome浏览器,需要先安装Chrome driver并将其路径添加到系统PATH中

    def test_search(self):
        self.driver.get("https://www.google.com")
        search_box = self.driver.find_element_by_name("q")
        search_box.send_keys("Python")
        search_box.submit()
        assert "Python" in self.driver.title

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

六、调试问题解决:
1.chrome 版本和路径查看:浏览器输入chrome://version/
2.chrome 驱动下载:
v114之前:https://chromedriver.storage.googleapis.com/index.html
v123之后:https://googlechromelabs.github.io/chrome-for-testing/
v115-122之间:https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints实在找不到可以在网址里更改版本号直接搜
3. 下载chromedriver.exe 后,需要把chrome.exe 和chromedriver.exe配置到环境变量里,否则执行的时候需要加参数指定chromedriver.exe
4. 加参数指定driver:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

class MyTest(unittest.TestCase):

    def setUp(self):
        # 指定webdriver路径
        self.driver = webdriver.Chrome(service=Service(r'/Users/yiyangjuan/Downloads/chromedriver-mac-x64/chromedriver'))

5.加参数指定Chrome:

options = webdriver.ChromeOptions()
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" 
self.browser = webdriver.Chrome(executable_path=chromedriver_path, options=options)

七、有用的参考链接:
webdriver.Chrome() 参数详解:https://blog.csdn.net/luckyjww/article/details/129689945
禁用chrome更新:https://blog.csdn.net/m0_73850058/article/details/135163838
操作浏览器代码常用方法:https://blog.csdn.net/Z_love_u/article/details/113645480
https://blog.csdn.net/IT_LanTian/article/details/122986725
截图:
https://blog.csdn.net/CYK_byte/article/details/128954079

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值