zoukankan      html  css  js  c++  java
  • Python3+Selenium自动化测试安装以及各种踩坑

    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:UsersTESTING-PCAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdrivercommonservice.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
    File "C:UsersTESTING-PCAppDataLocalProgramsPythonPython38-32libsubprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
    File "C:UsersTESTING-PCAppDataLocalProgramsPythonPython38-32libsubprocess.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

    问题描述二:

    Traceback (most recent call last):
    File "E:Projectpython-selenium-automationsample_script.py", line 6, in <module>
    driver = webdriver.Chrome()
    File "E:EnvironmentPythonpython3.9.2libsite-packagesseleniumwebdriverchromewebdriver.py", line 73, in __init__
    self.service.start()
    File "E:EnvironmentPythonpython3.9.2libsite-packagesseleniumwebdrivercommonservice.py", line 98, in start
    self.assert_process_still_running()
    File "E:EnvironmentPythonpython3.9.2libsite-packagesseleniumwebdrivercommonservice.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

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

      接着我们来到谷歌浏览器驱动的下载网址

    http://chromedriver.storage.googleapis.com/index.html

    ChromeDriver - WebDriver for Chrome - Downloads (chromium.org)

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

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

    卸载 最好使用360软件卸载 才能把注册表给卸载干净

    如果是控制面板手动卸载,则记得要删除注册表,cmd下输入regedit,找到software下的chrome相关内容进行删除

    官网找不到对应的版本 可以在这边下载

    Latest Releases (cypress.io)

    Google Chrome 64bit Windows版_chrome浏览器,chrome插件,谷歌浏览器下载,谈笑有鸿儒 (chromedownloads.net)

    Download older versions of Google Chrome for Windows, Linux and Mac (slimjet.com)

    下载完后可以直接安装

    问题描述三:

    我这边安装了但是无法正常使用 还是会报错报错内容和上面的还是一样,与是我下载了79版本的chrome和对应的driver 还是回出现上面的问题

    问题解决三:

    chromedriver.exe文件使用方法:

    (1)直接将下载好的chromedriver.exe文件复制粘贴到要执行的.py文件同级目录下就行。

     (2)将chromedriver.exe文件放在Chrome浏览器的目录下C:Program Files (x86)GoogleChromeApplication

    (3)配置环境变量在path中添加chromedriver.exe的路径

     

     (4)打开开始菜单->我的电脑(或计算机)->系统属性->高级系统设置->环境变量,编辑用户变量里的path, 在最后面添加;C:Program Files (x86)GoogleChromeApplication 或者在最前面添加C:Program Files (x86)GoogleChromeApplication; 总之变量之间用分号隔开,修改完之后点击确定按钮保存配置。

     (5)重启编译器,再次运行发现未报错,说明配置已生效。

    此时还要修改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.1chromedriver.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()



    声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/ 如出现转载未声明 将追究法律责任~谢谢合作
  • 相关阅读:
    Linux命令-文件管理(四)
    Linux命令-文件管理(三)
    Linux命令-文件管理(二)
    Linux命令-文件管理(一)
    Nodejs环境搭建
    Python csv文件操作
    python数组定义
    python3+Opencv 搭建环境和基本操作
    Python String startswith() Method
    Upload a file with $.ajax to AWS S3 with a pre-signed url
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/15294085.html
Copyright © 2011-2022 走看看