目录
Selenium也是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE、Mozilla Firefox、Mozilla Suite等
1.selenium资料与教程
selenium官网:https://www.seleniumhq.org/docs/index.jsp
python官方文档:http://selenium-python.readthedocs.io/index.html
selenium函数大全
https://www.cnblogs.com/zhaof/p/6953241.html
大神集成
Python+Selenium自动化测试从零到框架设计系列
https://blog.csdn.net/u011541946/article/category/6788788
2.Windows下安装Selenium
pip install selenium
安装Firefox驱动,下载地址 https://github.com/mozilla/geckodriver/releases
安装Chromedriver驱动:
https://sites.google.com/a/chromium.org/chromedriver/downloads
https://npm.taobao.org/mirrors/chromedriver/
下载后将驱动文件放在python scrip文件夹下即可。
Firefox驱程安装教程:https://www.cnblogs.com/glumer/p/6088258.html
注:记得把驱程文件也放在python script目录下
3.ubuntu下安装selenium
参考:
https://blog.csdn.net/qq_29303759/article/details/83719285
https://www.cnblogs.com/1510152012huang/p/9871147.html
(1)安装ubuntu版本chrome浏览器
sudo apt-get update
sudo apt-get install google-chrome-stable #如何失败继续往下安装
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get -f install
# sudo apt-get -f install 是修复损坏的软件包,尝试卸载出错的包,重新安装正确版本的。
/usr/bin/google-chrome-stable -version ##查看chrome版本号
(2)下载安装chromedriver驱动
#https://chromedriver.storage.googleapis.com/index.html?path=76.0.3809.68/
# wget https://chromedriver.storage.googleapis.com/76.0.3809.68/chromedriver_linux64.zip
wget https://chromedriver.storage.googleapis.com/80.0.3987.106/chromedriver_linux64.zip
(3)下载后解压并赋权
chmod 777 chromedriver
sudo mv chromedriver /usr/bin/
/usr/bin/chromedriver --version
sudo apt -y install libgconf2-4
(4)安装selenium
pip install selenium
4.mac下安装selenium
将下载好chromedriver驱动文件移动到/usr/local/bin目录中:
sudo mv chromedriver /usr/local/bin
5.selenium测试案例
from selenium import webdriver
#声明chrome浏览器对象
browser = webdriver.Chrome()
browser.get("http://www.baidu.com")
#通过id=kw定位到百度的输入框
browser.find_element_by_id("kw").send_keys("selenium")
#通过 id=su 定位到搜索按钮
browser.find_element_by_id("su").click()
browser.quit()
selenium 禁用显示 "DevTools on ws..."
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
6.headless无头selenium
from selenium import webdriver
options = webdriver.ChromeOptions()
# options.add_experimental_option('prefs', prefs)
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--hide-scrollbars')
# options.add_argument(self.user_agent)
# driver = webdriver.Chrome(executable_path="/Users/bing/desktop/chromedriver", options=options)
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options)
# driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
url='https://www.amazon.com/s?k=men+durags&page=1'
driver.get(url)
print(driver.title)
7.找不到chromedriver处理方法
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.
(1)方法一: 将chromedriver.exe文件复制到 "D:\Program Files\Anaconda3\Scripts" 目录下
(2)方法二: 程序指定chromedriver.exe位置
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
(3)方法三: 修改“\site-packages\selenium\webdriver\chrome”目录下的webdriver.py文件,如下:
8.Selenium IDE
selenium IDE结合浏览器提供脚本的录制,回放以及编辑脚本功能,以及元素的定位,可以使用selenium IDE将录制的脚本生成相应的带单元测试框架的自动化测试脚本。
#官网:https://www.selenium.dev/selenium-ide/
#CSDN:https://blog.csdn.net/heye13/article/details/79608081