[适合非python新手]selenium自动化测试 3-selenium最简单demo

2021/7/27 22:05:54

本文主要是介绍[适合非python新手]selenium自动化测试 3-selenium最简单demo,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

背景

沙箱安装selenium

demo编写

运行并查看效果


背景

上一篇已经教大家怎么创建一个python项目了。这一篇分享如何写selenium的hello world。

沙箱安装selenium

第一篇,我们装了一个selenium,那个是全局的,但是现在我们使用沙箱,所以需要再局部再次安装selenium。

如下:

(venv) O:\python\selenium-test>pip install selenium
Looking in indexes: https://pypi.doubanio.com/simple/
Collecting selenium
  Downloading https://pypi.doubanio.com/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904 kB)
     |████████████████████████████████| 904 kB 2.2 MB/s
Collecting urllib3
  Downloading https://pypi.doubanio.com/packages/9f/f0/a391d1463ebb1b233795cabfc0ef38d3db4442339de68f847026199e69d7/urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
     |████████████████████████████████| 127 kB 3.3 MB/s
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.25.10
WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.
You should consider upgrading via the 'o:\python\selenium-test\venv\scripts\python.exe -m pip install --upgrade pip' command.

demo编写

然后,在项目默认创建的main.py里头,删除所有默认的样例代码,输入如下内容:

from selenium import webdriver
 
 
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
 
print(driver.title)
 
driver.quit()

运行并查看效果

按照python程序的运行方式,运行main.py文件,笔者喜欢使用命令的方式:

python main.py

效果:

报错了

(venv) O:\python\selenium-test>python main.py
Traceback (most recent call last):
  File "o:\python\selenium-test\venv\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Users\32631\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "C:\Users\32631\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    driver = webdriver.Chrome()
  File "o:\python\selenium-test\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "o:\python\selenium-test\venv\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/h
ome

上述错误原因,是因为还缺乏一个自动化测试的浏览器插件。详见下一篇。



这篇关于[适合非python新手]selenium自动化测试 3-selenium最简单demo的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程