鼠标操作
键盘操作
下拉框
弹出框
滚动条
1 from time import sleep 2 from selenium import webdriver 3 4 driver = webdriver.Edge() 5 6 driver.get(‘file:///C:/Users/Administrator/Desktop/test/pagetest/%E6%B3%A8%E5%86%8CA.html‘) 7 8 js = "window.scrollTo(0,2000)" 9 driver.execute_script(js) 10 sleep(2) 11 js = "window.scrollTo(2000,100)" 12 driver.execute_script(js) 13 14 sleep(2) 15 16 driver.quit()
强制/隐式/显式等待
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from time import sleep 4 from selenium.webdriver.support.wait import WebDriverWait 5 6 driver = webdriver.Edge() 7 8 # 测试网站 9 driver.get(‘file:///C:/Users/Administrator/Desktop/test/pagetest/%E6%B3%A8%E5%86%8CA.html‘) 10 11 # 强制等待(加长的程序的运行时间,而且程序变得不可控) 12 # sleep(5) 13 # driver.find_element(‘xpath‘,‘/html/body/div/div[2]/div/input‘).send_keys(‘1231421‘) 14 # sleep(5) 15 # driver.find_element(‘xpath‘,‘/html/body/div/div[2]/div/input[2]‘).send_keys(‘fafasffa‘) 16 17 # 隐式等待(需要等待的我来等待,不需要等待的我直接加载不等待,期间会不断的去检查浏览器有没有加载出来,消耗资源大) 18 driver.implicitly_wait(2) 19 20 driver.find_element(‘xpath‘,‘/html/body/div/div[2]/div/input‘).send_keys(‘1231421‘) 21 driver.find_element(‘xpath‘,‘/html/body/div/div[2]/div/input[2]‘).send_keys(‘fafasffa‘) 22 23 # # 显示等待(需要等待的我来等待,不需要等待的我直接加载不等待,特定时间区去检查一次,消耗资源更少) 24 25 # element = WebDriverWait(driver,10,2).until(lambda x:x.find_element(‘xpath‘,‘/html/body/div/div[2]/div/input‘)) 26 # element = WebDriverWait(driver,5,2).until_not(lambda x:x.find_element(‘xpath‘,‘/html/body/div/div[2]/div/input‘)) 27 # element.send_keys(‘123123124‘) 28 29 # until(method, message=‘‘) 30 # 调用该方法体提供的回调函数作为一个参数,直到返回值为True 31 # until_not(method, message=‘‘) 32 # 调用该方法体提供的回调函数作为一个参数,直到返回值为False 33 34 sleep(8) 35 36 driver.close()