标签:全选 index 技术 ima drive console 快捷 script 字母
可以先在console中调试好
from selenium import webdriver
import time
driver = webdriver.Chrome(‘D:\\tools\chromedriver.exe‘)
driver.get(‘https://tieba.baidu.com/index.html‘)
#向右滚动100,向下滚动500
driver.execute_script(‘window.scrollBy(100,500)‘)
from selenium import webdriver
import 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 webdriver
import 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 webdriver
from selenium.webdriver.common.action_chains import ActionChains
import 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 webdriver
from selenium.webdriver.common.action_chains import ActionChains
import 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 webdriver
from selenium.webdriver.common.action_chains import ActionChains
import 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() #单击
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import 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()
标签:全选 index 技术 ima drive console 快捷 script 字母
原文地址:https://www.cnblogs.com/like1824/p/12966998.html