1. 程式人生 > 實用技巧 >Selenium-截圖

Selenium-截圖

報錯截圖

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()

driver.get('https://xdclass.net/')

print(driver.title)

sleep(5)

#點選登入按鈕後彈出登入視窗
login_ele = driver.find_element_by_css_selector('.login > span:nth-child(2)
') ActionChains(driver).click(login_ele).perform() #查詢輸入框,清除賬號輸入框快取,輸入賬號 driver.find_element_by_css_selector('.mobienum > input:nth-child(1)').clear() driver.find_element_by_css_selector('.mobienum > input:nth-child(1)').send_keys('13918238352') #清除密碼輸入框快取,輸入密碼 driver.find_element_by_css_selector('
.psw > input:nth-child(1)').clear() driver.find_element_by_css_selector('.psw > input:nth-child(1)').send_keys('sunjiawei') #點選登入按鈕 driver.find_element_by_css_selector('.btn').click() try: #選擇一個只有正確登入後的元素定位進行判斷是否登入成功 driver.find_element_by_css_selector('.loginin > span:nth-child(3) > a:nth-child(1)
').click() except: #如果登入失敗則擷取報錯圖片 driver.get_screenshot_as_file('D:\error.png')