元素定位
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.firefox.firefox_binary import FirefoxBinaryimport timebinary = FirefoxBinary('/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin')driver = webdriver.Firefox(firefox_binary=binary)driver.get('http://www.baidu.com')size = driver.find_element_by_id('kw').sizeprint(size)text = driver.find_element_by_id('cp').textprint(text)size1 = driver.find_element_by_id('kw').get_attribute('type')print(size1)size2 = driver.find_element_by_id('kw').is_displayed()print(size2)driver.find_element_by_id('kw').clear()driver.find_element_by_id('kw').send_keys('selenium')driver.find_element_by_id('su').click()time.sleep(2)driver.get('http://news.baidu.com')time.sleep(2)driver.back()time.sleep(2)driver.forward()time.sleep(2)driver.refresh()
鼠标键盘操作
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.firefox.firefox_binary import FirefoxBinaryfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.keys import Keysimport timebinary = FirefoxBinary('/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin')driver = webdriver.Firefox(firefox_binary=binary)driver.get('http://www.baidu.com')right_click = driver.find_element_by_id("listHolder")# 右键ActionChains(driver).context_click(right_click).perform()# 鼠标悬停above = driver.find_element_by_name("tj_settingicon")ActionChains(driver).move_to_element(above).perform()#双击double_click = driver.find_element_by_id('xx')ActionChains(driver).double_click(double_click).perform()# 拖动drag_start = driver.find_element_by_id('xx')drag_end = driver.find_element_by_id('cc')ActionChains(driver).drag_and_drop(drag_start,drag_end).perform()#输入框输入内容driver.find_element_by_id('kw').send_keys('hha')#删除多输入的内容driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)#输入空格driver.find_element_by_id('kw').send_keys(Keys.SPACE)# ctrl+a全选driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'a')#回车driver.find_element_by_id('kw').send_keys(Keys.ENTER)
显式/隐式等待
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.firefox.firefox_binary import FirefoxBinaryfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECbinary = FirefoxBinary('/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin')driver = webdriver.Firefox(firefox_binary=binary)driver.get('http://www.baidu.com')#显式等待element = WebDriverWait(driver,5,0.5).until(EC.presence_of_all_elements_located((By.ID,'kw')))element.send_keys('ss')#隐式等待from selenium.common.exceptions import NoSuchElementExceptiondriver.implicitly_wait(10)driver.get('http://www.baidu.com')try: driver.find_element_by_id('kw').send_keys('ss')except NoSuchElementException as e: print(e)
窗口/frame切换
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.firefox.firefox_binary import FirefoxBinarybinary = FirefoxBinary('/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin')driver = webdriver.Firefox(firefox_binary=binary)driver.get('http://www.baidu.com')#iframe切换id或name,否则用XPATH定位driver.switch_to.frame("if")#返回之前页面driver.switch_to.parent_frame()# 多窗口切换#获得当前窗口句柄search_windows=driver.current_window_handledriver.find_element_by_link_text('登录').click()driver.find_element_by_link_text('立即注册').click()#获取当前所有打开的窗口句柄all_handles = driver.window_handles#进入注册窗口for handle in all_handles: if handle != search_windows: driver.switch_to.window(handle) print('注册。。。') # 填写信息send_keys#回到之前窗口for handle in all_handles: if handle ==search_windows: driver.switch_to.window(handle) print('搜索。。。')
警告框处理
from selenium import webdriverfrom time import sleepdriver = webdriver.Chrome()driver.get("https://www.baidu.com")driver.find_element_by_link_text("设置").click()driver.find_element_by_class_name("setpref").click()sleep(2)# 保存设置driver.find_element_by_class_name("prefpanelgo").click()sleep(2)# 接受警告框driver.switch_to.alert().accept()driver.quit()
文件下载
from selenium import webdriverimport os# download.default_directory 指定文件下载路径。# profile.default_content_settings.popups 设置0,禁止下载时弹出窗口。options = webdriver.ChromeOptions()prefs = { 'profile.default_content_settings.popups': 0, 'download.default_directory': os.getcwd() }options.add_experimental_option('prefs', prefs)driver = webdriver.Chrome(chrome_options=options)driver.get("http://pypi.Python.org/pypi/selenium")driver.find_element_by_partial_link_text("selenium-3.0.2.tar.gz").click()
文件上传
from selenium import webdriverimport os, timedriver = webdriver.Chrome()file_path = 'file:///' + os.path.abspath('./webdriver_api/web_page/upfile.html')driver.get(file_path)time.sleep(2)# 定位上传按钮,添加本地文件driver.find_element_by_name("file").send_keys(os.path.abspath('./webdriver_api/web_page/upload_file.txt'))time.sleep(5)driver.quit()
操作cookie
from selenium import webdriverdriver = webdriver.Chrome()''' * get_cookies() 获得所有 cookie 信息。 * get_cookie(name) 返回字典的 key 为“ name”的 cookie 信息。 * add_cookie(cookie_dict) 添加 cookie。“ cookie_dict”指字典对象,必须有name和value 值。 * delete_cookie(name,optionsString) 删除 cookie 信息。“name”是要删除的 cookie 的名称, “optionsString”是该 cookie 的选项,目前支持的选项包括“路径”,“域”。 * delete_all_cookies() 删除所有 cookie 信息。'''driver.get("https://www.baidu.com")# 向 cookie 的 name 和 value 中添加会话信息driver.add_cookie({ 'name': 'key-aaaaaaa', 'value': 'value-bbbbbb'})# 遍历 cookies 中的 name 和 value 信息并打印,当然还有上面添加的信息for cookie in driver.get_cookies(): print("%s -> %s" % (cookie['name'], cookie['value']))driver.quit()
调用javascript
from selenium import webdriverfrom time import sleep#execute_script() 调用JavaScript操作Web。driver=webdriver.Chrome()driver.get("https://www.baidu.com")driver.set_window_size(600, 600)driver.find_element_by_id("kw").send_keys("selenium")driver.find_element_by_id("su").click()sleep(2)# 通过 javascript 设置浏览器窗口的滚动条位置js="window.scrollTo(100,450);"driver.execute_script(js)sleep(3)driver.quit()
窗口截图
from selenium import webdriverfrom time import sleepimport os# get_screenshot_as_file() 截取窗口图片。driver = webdriver.Chrome()driver.get('https://www.baidu.com')driver.find_element_by_id('kw').send_keys('selenium')driver.find_element_by_id('su').click()sleep(2)# 截取当前窗口,并指定截图图片的保存位置driver.get_screenshot_as_file(os.path.abspath('./webdriver_api/web_page/baidu_page.jpg'))driver.quit()
视频播放处理
from selenium import webdriverfrom time import sleepdriver = webdriver.Chrome()driver.get("http://videojs.com/")video = driver.find_element_by_id("home_video")# 返回播放文件地址url = driver.execute_script("return arguments[0].currentSrc;", video)print(url)# 播放视频print("start")driver.execute_script("return arguments[0].play()", video)# 播放 15 秒钟sleep(15)# 暂停视频print("stop")driver.execute_script("arguments[0].pause()", video)driver.quit()
下拉框选择
from selenium import webdriverfrom selenium.webdriver.support.select import Selectimport osdriver = webdriver.Chrome()file_path = 'file:///' + os.path.abspath('./webdriver_api/web_page/select_tag.html')driver.get(file_path)sel = driver.find_element_by_xpath("//select[@id='status']")Select(sel).select_by_value('0') #未审核Select(sel).select_by_value('1') #初审通过Select(sel).select_by_value('2') #复审通过Select(sel).select_by_value('3') #审核不通过