selenium界面特殊操作

滚动操作

可以先在console中调试好

 selenium界面特殊操作

from selenium import webdriverimport time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘https://tieba.baidu.com/index.html‘)#向右滚动100,向下滚动500driver.execute_script(‘window.scrollBy(100,500)‘)

窗口截图

from selenium import webdriverimport time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘http://baidu.com‘)#截屏,截取整个页面driver.get_screenshot_as_file(‘all.png‘)#截屏,截取特定的某个元素ele = driver.find_element_by_id(‘kw‘)
ele.screenshot(‘ele.png‘)

警告框

from selenium import webdriverimport time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘C:\\Users\johnxuan\Desktop\like\code\\basic\\test4.html‘)‘‘‘
#触发对话框
driver.find_element_by_id(‘bu1‘).click()
#获取警告框
a1 = driver.switch_to.alert
#确认对话框
a1.accept()
time.sleep(3)
‘‘‘
‘‘‘
#触发确认框
driver.find_element_by_id(‘bu2‘).click()
#获取确认框
a2 = driver.switch_to.alert
#确认对话框
time.sleep(1)
a2.accept()    #点击确认
#取消对话框
time.sleep(1)
driver.find_element_by_id(‘bu2‘).click()
time.sleep(2)
a2.dismiss()   #点击取消
‘‘‘#触发提示框driver.find_element_by_id(‘bu3‘).click()#获取提示框a3 = driver.switch_to.alert#向提示框中输入内容a3.send_keys(‘hahhahhah‘)
time.sleep(3)#确认对话框a3.accept()#取消对话框driver.find_element_by_id(‘bu3‘).click()
a3.dismiss()
driver.quit()

鼠标事件

悬停

from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘http://www.baidu.com‘)#定位到要悬停的元素ele = driver.find_element_by_id(‘s-usersetting-top‘)
time.sleep(2)#对定位到的元素进行鼠标悬停操作ActionChains(driver).move_to_element(ele).perform()
time.sleep(3)
driver.quit()

拖拽

from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘C:\\Users\johnxuan\Desktop\like\code\\basic\\test.html‘)#定位到目标元素Targetele = driver.find_element_by_id(‘targetele‘)#定位到起始元素Startele = driver.find_element_by_id(‘startele‘)#元素拖拽,将起始元素拖拽到目标元素ActionChains(driver).drag_and_drop(Startele,Targetele).perform()

右键、双击、点击

from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘C:\\Users\johnxuan\Desktop\like\code\\basic\\test.html‘)#定位元素ele = driver.find_element_by_id(‘s-usersetting-top‘)#操作ActionChains(driver).context_click(ele).perform()   #右击ActionChains(driver).double_click(ele).perform()    #双击ActionChains(driver).click(ele).perform()    #单击

键盘事件

 selenium界面特殊操作 

from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘http://www.baidu.com‘)#定位到输入框inpEl = driver.find_element_by_id(‘kw‘)#输入内容inpEl.send_keys(‘selenium‘)
time.sleep(2)#删除最后一个字母# inpEl.send_keys(Keys.BACK_SPACE)
# time.sleep(2)
#输入一个空格# inpEl.send_keys(Keys.SPACE)
# inpEl.send_keys(‘安装‘)
# time.sleep(2)
# #快捷键 Ctrl + A 全选输入框内容# inpEl.send_keys(Keys.CONTROL,‘a‘)
# time.sleep(1)
# #快捷键 Ctrl + X 剪切输入框内容# inpEl.send_keys(Keys.CONTROL,‘x‘)
# time.sleep(1)driver.quit()

相关推荐