元素定位javascript
#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()
鼠標鍵盤操做html
#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)
顯式/隱式等待java
#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切換web
#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('搜索。。。')
警告框處理chrome
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()
文件下載windows
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()
文件上傳api
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()
調用javascriptcookie
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()
窗口截圖app
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') #審覈不經過