selenium + win32api + pyautogui
元素定位(可无头进行访问,但是会被检测,基本用不了)
sli_ele = driver.find_element(By.XPATH, '//span[@id="nc_1_n1z"]')#xpath 定位
if sli_ele.is_displayed():
ActionChains(driver).click_and_hold(on_element=sli_ele).perform()#长按左键
ActionChains(driver).move_by_offset(xoffset=260, yoffset=0).perform()#拖拽到指定位置
time.sleep(2)
ActionChains(driver).pause(0.5).release().perform() #执行
time.sleep(2)
win32api(拖拽方式,mac系统不可用)
win32api.SetCursorPos((450, 410))#定位坐标
time.sleep(2)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 450, 410) # 点击
time.sleep(0.5)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 300, 0) # 移动
time.sleep(1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) # 松开鼠标
pyautogui模块(局限需要开启浏览器进行滑动,暂时看支持mac和windows,要比wei32好)
pyautogui.PAUSE = 1 # 调用在执行动作后暂停的秒数,只能在执行一些pyautogui动作后才能使用,建议用time.sleep
pyautogui.FAILSAFE = False
x,y = pyautogui.locateCenterOnScreen("aj.png", confidence=0.8) #打开图片,进行写,y定位
print(x,y)
pyautogui.moveTo(x,y, duration=1) #定位元素 可以将x,y写入,duration 是移动时间
pyautogui.mouseDown() #长按左键
pyautogui.dragTo(x,random.choice([450,550]), duration=0.5,button='left') #进行滑动移动,y可以写死也可以写随机数,duration移动时间,button是mac系统需要填写
代码
import re
import os
import time
import random
import pyautogui
from lxml import etree
from selenium import webdriver
def drivers(headless=None,procxy=None,time=30):
'''
初始化driver
'''
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('disable-infobars')
option.add_argument('--disable-gpu')
option.add_argument('--lang=zh-cn')
option.add_argument('--no-sandbox')
option.add_argument('disable-dev-shm-usage')
option.add_argument("--no-sandbox")
option.add_argument("--disable-dev-shm-usage")
option.add_argument("--window-size=1920x1080")
option.add_argument("start-maximised")
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument("--disable-blink-features=AutomationControlled")
# option.add_argument('--proxy-server=' + get_procxy()["http"])
driver = webdriver.Chrome(options=option)
driver.set_page_load_timeout(time)
driver.set_script_timeout(40)
# 全屏,为了方便点击,有些页面由于不是自适应所有全屏操作更为保险
# driver.maximize_window()
return driver
def action(url):
driver.get(url)
time.sleep(5)
driver.implicitly_wait(10)
resp_html = driver.execute_script("return document.documentElement.outerHTML")
sli_ele = driver.find_element(By.XPATH, '//span[@id="nc_1_n1z"]')
if sli_ele.is_displayed():
ActionChains(driver).click_and_hold(on_element=sli_ele).perform()
ActionChains(driver).move_by_offset(xoffset=260, yoffset=0).perform()
time.sleep(2)
ActionChains(driver).pause(0.5).release().perform()
time.sleep(2)
if __name__ == '__main__':
url = 'qwertyuiopasdfghjkl;zxcvbnm,' #你懂的
driver = drivers()
driver.get(url)
res = driver.execute_script("return document.documentElement.outerHTML")
resp_url_list = res.xpath('//div[@class="wow zoomIn centerList el-col el-col-12"]/a/@href')
print(resp_url_list)
driver.quit()