selenium webdriver API
元素定位
#coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary import time binary = 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').size print(size) text = driver.find_element_by_id('cp').text print(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-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys import time binary = 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-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC binary = 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 NoSuchElementException driver.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-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = 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_handle driver.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 webdriver from time import sleep driver = 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 webdriver import 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 webdriver import os, time driver = 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 webdriver driver = 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 webdriver from 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 webdriver from time import sleep import 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 webdriver from time import sleep driver = 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 webdriver from selenium.webdriver.support.select import Select import os driver = 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') #审核不通过