clear() #清楚輸入框的內容 send_keys('內容') #在文本框內輸入內容 click() #點擊按鈕 submit() #表單的提交
ActionChains模塊中的方法:
click(on_element=None) #單擊鼠標左鍵
click_and_hold(on_element=None) #點擊鼠標左鍵,按住不放
context_click(on_element=None) #點擊鼠標右鍵
double_click(on_element=None) #雙擊鼠標左鍵
drag_and_drop(source, target) #拖拽到某個元素然后松開
drag_and_drop_by_offset(source, xoffset, yoffset) #拖拽到某個坐標然后松開
move_by_offset(xoffset, yoffset) #鼠標移動到距離當前位置(x,y)
move_to_element(to_element) #鼠標移動到某個元素
move_to_element_with_offset(to_element, xoffset, yoffset) #將鼠標移動到距某個元素多少距離的位置
release(on_element=None) #在某個元素位置松開鼠標左鍵
perform() #執行鏈中的所有動作
ActionChains的兩種寫法:
#首先導入模塊 from slenium.webdriver.common.action_chains import ActionChins #鏈條式方法 searchElement = driver.find_element_by_id('sb_form_q').send_keys('selenium') searchButtonElement = driver.find_element_by_id('sb_form_go') ActionChains(driver).click(searchButtonElement).perform() #分布式方法 searchElement = driver.find_element_by_id('sb_form_q').send_keys('selenium') searchButtonElement = driver.find_element_by_id('sb_form_go') ActionChainsDriver = ActionChains(driver).click(searchButtonElement) ActionChainsDriver.perform()
在12306主頁做一個練習,效果如gif
from selenium.webdriver.common.action_chains import ActionChains from selenium import webdriver from time import sleep get_12306 = webdriver.Firefox() get_12306.get('https://www.12306.cn/index/index.html') g_href = get_12306.find_element_by_xpath('//*[@id="J-index"]/a') Action = ActionChains(get_12306) for x in range(9): x = x * 145 print(x) Action.move_to_element_with_offset(g_href, x, 0).perform() sleep(0.5) sleep(2) get_12306.quit()