一、概念:
selenium是一个自动化测试框架,主要用到webdriver。
二、webdriver(支持各大主流浏览器),chromedriver安装:
下载chromedriver压缩包,下载地址:ChromeDriver官方网站或ChromeDriver仓库
注:国内不能直接访问Chrome官网,可以在ChromeDriver仓库中下载(ChromeDriver与Chrome版本要对应)
window下建议将程序放到谷歌浏览器安装目录下(例如:C:\Program Files (x86)\Google\Chrome\Application,并将这个目录添加到环境变量中)
linux系统下添加环境变量(chromedriver假设在/home/user/下):
export PATH="$PATH:/home/user"
如果不设置环境变量,则需要在启动的时候指定路径
from selenium import webdriver
from selenium.webdriver import ChromeOptions
#实例化chromedriver
def get_chromedriver():
#设置参数
chrome_options = ChromeOptions()
chrome_options.add_argument(\'--headless\')#控制是否显示浏览器,注释将显示
#chrome_options.add_argument("--start-maximized")#控制浏览器运行时的窗口大小
chrome_options.add_argument(\'--disable-gpu\')
driverpath = "D:\chromedriver.exe"#下载的chromedriver.exe的全路径
#创建谷歌浏览器驱动对象,用于后续操作浏览器
chromedriver = webdriver.Chrome(executable_path=driverpath, options=chrome_options)
return chromedriver
三、启动浏览器
from selenium import webdriver
driver = webdriver.Chrome()
# 指定路径启动
driver = webdriver.Chrome(executable_path =\'D:/chromedriver.exe\')
# from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions
# option = Options()
option = ChromeOptions()
option.add_argument(\'--headless\') #控制是否显示浏览器,注释将显示
# 将插件xpath应用扩展加到浏览器中
extension_path = \'D:/VM/2.0.2_0.crx\'
option.add_extension(extension_path)
# 禁用浏览器正在被自动化程序控制的提示
option.add_argument(\'--disable-infobars\')
driver = webdriver.Chrome(options=option)
# driver = webdriver.PhantomJS() # 无界面浏览已停止更新,建议使用headless
四、控制浏览器
# 范文某个页面
driver.get(\'https://www.baidu.com\')
# 刷新
driver.refresh()
# 后退
driver.back()
# 前进
driver.forward()
# 退出
driver.quit()
# 当前url地址
driver.current_url
# 截图
driver.save_screenshot(\'./save.png\')
# 窗口最大化
driver.maximize_window()
五、元素查找
# 根据css选择器查找
driver.find_element_by_css_selector()
# 根据元素id查找
driver.find_element_by_id()
# 利用xpath表达式查找
driver.find_element_by_xpath()
# 根据name属性查找
driver.find_element_by_name()
# 根据class属性查找
driver.find_element_by_class_name()
# 根据tag标签名字查找
driver.find_element_by_tag_name()
# 根据链接文本查找,全词匹配
driver.find_element_by_link_text()
# 根据链接文本查找,部分词匹配即可
driver.find_element_by_partial_link_text()
以上函数返回结果为单个WebElement对象,并且还有复数elements,例如:
driver.find_elements_by_xpath()
返回结果为列表,包含若干个WebElement对象,如果找不到则抛出异常
六、等待wait相关
# 隐式等待,如果没有立即查到某个元素,则等待10秒
driver.implicitly_wait(10)
# 显示等待(常使用)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# 查找一个按钮,最长等待10秒,直到找到查找条件中指定的元素
find_btn= WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,\'//div/a[2]\')))
七、WebElement
和上面五条类似也有很多find函数
其它函数:
click:点击某个元素
send_keys:
# 向input框输入内容
input.send_keys(\'内容\')
# 发送一个回车键
from selenium.webdriver.common.keys import Keys
input.send_keys(Keys.RETURN)
rect:返回元素的宽和高,以及在屏幕上的坐标
>>> driver.find_element_by_id(\'su\').rect
{\'height\': 44, \'width\': 108, \'x\': 844, \'y\': 219}
location:返回在屏幕上的坐标
>>> driver.find_element_by_id(\'su\').location
{\'x\': 844, \'y\': 219}
text:返回查找元素内的文本信息,注意:用xpath表达式不可直接查找文本,只能用节点,再用text取文本
>>> driver.find_element_by_xpath(\'//div[@id="s-top-left"]/a[1]\').text
\'新闻\' 郑州祛胎记医院哪家好 http://m.zykdtj.com/
get_attribute和get_property:获取元素属性值,要先写到节点,再用此方法
>>> driver.find_element_by_xpath(\'//div[@id="s-top-left"]/a[1]\').get_property(\'href\')
\'http://news.baidu.com/\'
>>> driver.find_element_by_xpath(\'//div[@id="s-top-left"]/a[1]\').get_attribute(\'href\')
\'http://news.baidu.com/\'
value_of_css_property:获取节点的CSS样式属性,直接写css样式属性
>>> driver.find_element_by_xpath(\'//div[@id="s-top-left"]/a[1]\').value_of_css_property(\'color\')
\'rgba(34, 34, 34, 1)\'
>>> driver.find_element_by_xpath(\'//div[@id="s-top-left"]/a[1]\').value_of_css_property(\'display\')
\'inline-block\'
page_source:获取页面源码
get_cookie/get_cookies:获取服务器返回的cookie
# 获取cookie可以通过内置的函数get_cookies(),所有get_cookies()返回值是由字典组成的列表。
>>> driver.get_cookies()
[{\'domain\': \'.baidu.com\', \'httpOnly\': False, \'name\': \'H_PS_PSSID\', \'path\': \'/\', \'secure\': False, \'value\': \'32288_1462_32439_32379_32355_32045_32393_32429_32115_32430\'},
{\'domain\': \'.baidu.com\', \'expiry\': 16270826.868372, \'httpOnly\': False, \'name\': \'BAIDUID\', \'path\': \'/\', \'secure\': False, \'value\': \'511D707AFD063BF502B538461401:FG=1\'},
{\'domain\': \'.baidu.com\', \'expiry\': 3743908473.868326, \'httpOnly\': False, \'name\': \'BIDUPSID\', \'path\': \'/\', \'secure\': False, \'value\': \'511D707AFD063BF57F7240F1FC3E5\'}, {\'domain\': \'.baidu.com\', \'expiry\': 37438473.868351, \'httpOnly\': False, \'name\': \'PSTM\', \'path\': \'/\', \'secure\': False, \'value\': \'15964688\'},
{\'domain\': \'www.baidu.com\', \'expiry\': 15288828, \'httpOnly\': False, \'name\': \'BD_UPN\', \'path\': \'/\', \'secure\': False, \'value\': \'123353\'},
{\'domain\': \'www.baidu.com\', \'httpOnly\': False, \'name\': \'BD_HOME\', \'path\': \'/\', \'secure\': False, \'value\': \'1\'}]
# 设置新的cookie
>>> driver.add_cookie({\'name\':\'lianxi\',\'value\':\'lili\'})
>>> driver.get_cookies()
结果其中包含:
{\'domain\': \'www.baidu.com\', \'expiry\': 22272361, \'httpOnly\': False,
\'name\': \'lianxi\', \'path\': \'/\', \'secure\': True, \'value\': \'lili\'}
八、执行JS
# 滚动条到底
driver.execute_script(\'window.scrollTo(0,document.body.scrollHeight)\')
# 滚动到某个元素可见
next_page = driver.find_element_by_css_selector(\'a.pn-next\')
driver.execute_script("return arguments[0].scrollIntoView();", next_page)
# 执行异步js函数
driver.execute_async_script(\'send_xml_request()\')