一、环境准备:
LINUX 安装chrome for linux
官网chrome版本地址:https://www.google.cn/intl/zh-CN_ALL/chrome/
驱动下载 https://npm.taobao.org/mirrors/chromedriver
yum安装方式
firefox(火狐) sudo yum install firefox
chrome(谷歌)
添加源:sudo wget http://repo.fdzh.org/chrome/google-chrome-mirrors.repo -P /etc/yum.repos.d/
安装:sudo yum install google-chrome-stable
查看chromedriver版本
chromedriver --version
ChromeDriver 70.0.3538.97
查看chrome版本
yum list installed|grep chrom
google-chrome-stable.x86_64 70.0.3538.77-1
from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument('--headless') #chrome_options.add_argument('--disable-dev-shm-usage') driver=webdriver.Chrome(chrome_options=chrome_options) driver.get('https://www.baidu.com') title=driver.title print(title) driver.quit()
报错提示:(unknown error: DevToolsActivePort file doesn't exist)
添加代码
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--headless')
WIN
windows10 python3 selenium
pip install selenium
chrome 驱动下载根据浏览器版本选择 https://npm.taobao.org/mirrors/chromedriver
ChromeDriver v2.42 (2018-09-13)----------Supports Chrome v68-70
ChromeDriver v2.41 (2018-07-27)----------Supports Chrome v67-69
ChromeDriver v2.40 (2018-06-07)----------Supports Chrome v66-68
ChromeDriver v2.39 (2018-05-30)----------Supports Chrome v66-68
ChromeDriver v2.38 (2018-04-17)----------Supports Chrome v65-67
ChromeDriver v2.37 (2018-03-16)----------Supports Chrome v64-66
ChromeDriver v2.36 (2018-03-02)----------Supports Chrome v63-65
ChromeDriver v2.35 (2018-01-10)----------Supports Chrome v62-64
二、相关方法
单个元素定位方法
- find_element_by_id()
- find_element_by_name()
- find_element_by_class_name()
- find_element_by_tag_name()
- find_element_by_link_text()
- find_element_by_partial_link_text()
- find_element_by_xpath()
- find_element_by_css_selector()
多个元素定位:
- find_elements_by_id()
- find_elements_by_name()
- find_elements_by_class_name()
- find_elements_by_tag_name()
- find_elements_by_link_text()
- find_elements_by_partial_link_text()
- find_elements_by_xpath()
- find_elements_by_css_selector()
driver.refresh() 刷新
clear(): 清除文本。
send_keys (value): 模拟按键输入。
click(): 单击元素。
submit() 提交
窗口切换
- current_window_handle:获得当前窗口句柄。
- window_handles:返回所有窗口的句柄到当前会话。
- switch_to.window():用于切换到相应的窗口,与上一节的switch_to.frame()类似,前者用于不同窗口的切换,后者用于不同表单之间的切换
三、例子解析
例子1:登陆v2ex 网站,验证码问题需解决
#coding:utf-8 from selenium import webdriver driver=webdriver.Chrome() driver.get('https://www.v2ex.com/') driver.find_element_by_link_text("登录").click() username=driver.find_element_by_css_selector("#Main > div.box > div.cell > form > table > tbody > tr:nth-child(1) > td:nth-child(2) > input") username.send_keys('test') passwd=driver.find_element_by_css_selector("#Main > div.box > div.cell > form > table > tbody > tr:nth-child(2) > td:nth-child(2) > input") passwd.send_keys('11111') captcha=driver.find_element_by_css_selector("#Main > div.box > div.cell > form > table > tbody > tr:nth-child(3) > td:nth-child(2) > input") captcha.send_keys('dddd') captcha.submit()
例子2:无验证码登陆
from selenium import webdriver from selenium.webdriver.support.select import Select driver=webdriver.Chrome() handle_t=driver.current_window_handle driver.get('https://www.myoffer.cn') # driver.get('https://www.myoffer.cn/account/login?callbackRedirect=%2F') driver.find_element_by_css_selector("body > header > div > div.my-top > div.li-bt-login > a").click() driver.find_element_by_css_selector("#login-enroll-user-select").click() driver.find_element_by_css_selector("#login-form-login-btn").click() driver.find_element_by_css_selector("#login-user").click() user=driver.find_element_by_css_selector("#login-user") user.send_keys('xxxx') passwd=driver.find_element_by_css_selector("#login-password") passwd.send_keys('xxxx') driver.find_element_by_css_selector("#login-form-login > div.login-form-container > div > div.login-form-1 > div:nth-child(3) > button").click()
参考:https://blog.csdn.net/zhuyiquan/article/details/79537623