实际工作中,按照用户操作习惯,进行的是模拟键盘和鼠标操作WEB。
首先,导入键盘操作需要的服务
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2023/6/26 17:32 # @File : P01-test-selenium.py # @Software: PyCharm from selenium.webdriver import Keys def P01(): import time from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait # 导入等待机制显示等待需要的包 from selenium import webdriver # 导入selenium的webdriver包 from selenium.webdriver.common.keys import Keys # 导入键盘服务包 from selenium.webdriver import ActionChains # 导入事务包 from selenium.webdriver.common.actions.action_builder import ActionBuilder driver = webdriver.Edge() # 设置浏览器 # driver.implicitly_wait(10) # 设置隐式等待10S wait = WebDriverWait(driver, 5) # 设置wait是驱动等待5秒才超时 driver.maximize_window() # 最大化网页窗口 driver.get('http://novel.hctestedu.com/') # 打开读书屋网址 # el = (By.CLASS_NAME, "sj_link") # 设置元素寻找方式(书架) # el = (By.ID, "searchKey") # 设置元素寻找方式(小说搜索) # el = (By.NAME, "searchKey") # 设置元素寻找方式(小说搜索) # el = (By.CLASS_NAME, "s_int") # 设置元素寻找方式(小说搜索) # el = (By.XPATH, "//body/div//div[@class='search cf']/input") # 设置元素寻找方式(小说搜索)--通过XPATH # el = (By.XPATH, "//*[@class='s_int' and @name='searchKey']") # 设置元素寻找方式(小说搜索)-通过XPATH多个属性定位 # el = (By.XPATH, "//a[text()='我的书架']") # 设置元素寻找方式(小说搜索)-通过XPATH的标签文本定位() # wait.until(ec.presence_of_element_located(el)).click() # el = (By.CSS_SELECTOR, "input#searchKey") # 设置元素寻找方式()--通过css-id # el = (By.CSS_SELECTOR, "input.s_int") # 设置元素寻找方式(小说搜索)--通过css-class # el = (By.CSS_SELECTOR, "input[name='searchKey']") # 设置元素寻找方式(小说搜索)--通过css-属性定位name # el = (By.LINK_TEXT, "排行榜") # 设置元素寻找方式(小说搜索)--通过文本 # el = (By.PARTIAL_LINK_TEXT, "排行") # 设置元素寻找方式(小说搜索)--通过模糊文本 # wait.until(ec.presence_of_element_located(el)).click() # wait.until(ec.presence_of_element_located(el)).send_keys("大王" + Keys.ENTER) # 通过显示等待寻找元素并点击回车键位 # locator = (By.CSS_SELECTOR, ".s_int") # wait.until(ec.presence_of_element_located(locator)) ele = driver.find_element(By.CSS_SELECTOR, ".s_int") # ===========暂停================ ActionChains(driver) \ .move_to_element(ele) \ .pause(1) \ .click_and_hold() \ .pause(1) \ .send_keys("abc") \ .perform() time.sleep(5) # 按下shift键 ActionChains(driver) \ .key_down(Keys.SHIFT) \ .perform() # 释放所有action ActionBuilder(driver).clear_actions() # 未释放是大写,释放后是小写 ActionChains(driver) \ .send_keys("d") \ .perform() time.sleep(5) driver.close() if __name__ == '__main__': P01()
标签:WEB,webdriver,el,selenium,driver,键盘,自动化,import,wait From: https://www.cnblogs.com/gezirui/p/17544257.html