Python3+Selenium自动化测试安装

1.python安装

python官网地址https://www.python.org/

在Windows上安装python,我下载的是64位安装程序

特别要注意勾上“Add Python 3.8 to PATH”,然后点“install now”即可完成安装

2.Selenium安装

使用python可直接利用pip进行安装selenium

启动cmd,注意:需要以管理员身份运行

Pip install -U selenium

测试驱动driver安装

Google Chrome driver:https://sites.google.com/a/chromium.org/chromedriver/downloads

我用的是谷歌浏览器,下载下来的zip文件解压至谷歌浏览器安装目录中

测试驱动driver测试

CMD中启动python并从selenium引入webdriver包:

from selenium import webdriver

驱动chrome浏览器

(1)Ch_driver = webdriver.Chrome()

(2)Ch_driver.get(“https://www.google.com”)

(3)Ch_driver.quit() # 使用quit()关闭了chrome并结束了此次测试,如果是close()只是关闭chrome,后台仍在进行。

踩坑

python执行pip install 命令的时候出现 File"",line 1 pip install 的问题

原因:pip是Python 包管理工具,该工具提供了对Python包的查找,下载,安装,卸载的功能,是直接在cmd中运行的,不需要进入到python中运行

解决方法:关掉当前cmd窗口,重新进入,不需要进入python,直接输入pip命令即可

解决 FileNotFoundError: [WinError 2] 系统找不到指定的文件

 

3.问题

问题描述一:

Ch_driver = webdriver.Chrome()

Traceback (most recent call last):

File "C:\Users\TESTING-PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start

self.process = subprocess.Popen(cmd, env=self.env,

File "C:\Users\TESTING-PC\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__

self._execute_child(args, executable, preexec_fn, close_fds,

File "C:\Users\TESTING-PC\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child

hp, ht, pid, tid = _winapi.CreateProcess(executable, args,

FileNotFoundError: [WinError 2] 系统找不到指定的文件。

问题解决一:

根据提示找到lib中的subprocess.py文件,CTRL+f查找class Popen模块,再将这个模块中的

__init__函数中的shell = False 改成shell = True

Python3+Selenium自动化测试安装以及各种踩坑_chrome

 

 

 

 

 

问题描述二:

Traceback (most recent call last):

File "E:\Project\python-selenium-automation\sample_script.py", line 6, in <module>

driver = webdriver.Chrome()

File "E:\Environment\Python\python3.9.2\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__

self.service.start()

File "E:\Environment\Python\python3.9.2\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start

self.assert_process_still_running()

File "E:\Environment\Python\python3.9.2\lib\site-packages\selenium\webdriver\common\service.py", line 109, in assert_process_still_running

raise WebDriverException(

selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1

 

问题解决二:

关于chromedriver的版本和chrome浏览器版本不匹配,需要对应。

先查看chrome浏览器版本

在chrome浏览器地搜索栏中输入chrome://version

Python3+Selenium自动化测试安装以及各种踩坑_版本号_02

 

 

可以看到当前版本是 93.0.4577.82,下载对应的版本号 放入我们的浏览器 安装的文件夹里面

  接着我们来到谷歌浏览器驱动的下载网址​​http://chromedriver.storage.googleapis.com/index.html​

 

不过在寻找chromedriver中会遇到另一种情况,就是chrome浏览器版本太新了,以至于没有相应的chromedriver版本号。

这种情况就要重新安装旧版本chrome浏览器,使之与chromedriver版本相匹配。记得先卸载原有的chrome浏览器。

 

此时还要修改python代码

修改代码如下




from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

import time
chrome_driver = 'D:\python3.8.1\chromedriver.exe'
browser = webdriver.Chrome(executable_path=chrome_driver)
time.sleep(1)

try:
browser.get('https://www.baidu.com')
input = browser.find_element_by_id('kw')
input.send_keys('Python')
input.send_keys(Keys.ENTER)
wait = WebDriverWait(browser,10)
wait.until(EC.presence_of_element_located((By.ID,'content_left')))
print(browser.current_url)
print(browser.get_cookies())
print(browser.page_source)
finally:
browser.close()