一:可以使用selenium去点击
selenium默认的点击操作是点元素0,0点坐标,那么我们可以在元素x,y坐标上加5,让点击操作往里一点。
from selenium.webdriver.common.action_chains import ActionChains ActionChains(self.driver).move_to_element(ele).move_by_offset(5, 5).click().perform() |
二:可以使用模拟键盘模块去使用,例如selenium,大漠插件,pykeyboard等模块
1、寻找附近比较近的输入框之类,比较显眼的,在通过模拟tab键聚焦要点击元素位置上,在模拟回车键,代码如下:
# 这边是用大漠写的一个例子 其余模块大致逻辑是一样的,不建议使用selenium driver.find_element_by_xpath('//*[@id="app"]/div/div/form/div[2]/div/div/input').click() time.sleep(3) dm.tab() time.sleep(3) dm.enter() |
2、像是那种登录(没有验证码之类)的直接在密码输入框直接模拟回车键 一般都会直接点击的,代码如下:
driver.find_element_by_xpath('//*[@id="app"]/div/div/form/div[2]/div/div/input').click() driver.find_element_by_xpath('//*[@id="app"]/div/div/form/div[2]/div/div/input').send_keys(Keys.ENTER) |
3 执行js代码,代码如下:
driver.execute_script("next()") |
三:实现了大漠了模拟鼠标和键盘的一个工具类
win32com 直接安装pywin32就可以,如果安装不成功 自行百度吧
import win32com import random import time dm_key_str = {'1': 49, '2': 50, '3': 51, '4': 52, '5': 53, '6': 54, '7': 55, '8': 56, '9': 57, '0': 48, '-': 189, '=': 187, 'back': 8, 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, 'ctrl': 17, 'alt': 18, 'shift': 16, ' ': 32, 'tab': 9, '`': 192, 'enter': 13, 'esc': 27, 'up': 38, 'down': 40, 'left': 37, 'right': 39, 'delete': 46, 'end': 35, 'pgup': 33, 'pgdn': 34, '[': 219, ']': 221, '\\': 220, ';': 186, "'": 222, ',': 188, '.': 190, '/': 191} shift_key = {'_': '-', '!': '1', '@': '2', '#': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0', '+': '=', '{': '[', '}': ']', ':': ';', '"': "'", '<': ',', '>': '.', '?': '/', '~': '`'} class DM(): """ DM 模拟鼠标键盘真实点击输入 """ def __init__(self): self.dm = win32com.client.Dispatch('dm.dmsoft') def moveto(self, x, y): self.dm.MoveTo(x, y) def leftclick(self): self.dm.LeftClick() def rightclick(self): self.dm.RightClick() def backspace(self): self.dm.KeyPress(8) def enter(self): self.dm.KeyPress(13) def tab(self): self.dm.KeyPress(9) def pgup(self): self.dm.KeyPress(33) def pgdn(self): self.dm.KeyPress(34) def up(self): self.dm.KeyPress(38) def down(self): self.dm.KeyPress(40) def left(self): self.dm.KeyPress(37) def right(self): self.dm.KeyPress(39) def sendstr(self, data): res = list(data) for x in res: self.input(x) time.sleep(random.uniform(0.2, 1)) def input(self, k): if k.isupper(): k = k.lower() self.dm.KeyDown(16) self.input(k) self.dm.KeyUp(16) elif k in shift_key: self.dm.KeyDown(16) self.input(shift_key[k]) self.dm.KeyUp(16) else: if k not in dm_key_str: raise Exception('dm驱动暂时还未支持字符 %s 的输入' % k) self.dm.KeyPress(dm_key_str[k]) |
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理