这是全部的代码
from selenium import webdriver
import requests
from lxml import etree
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ChromeOptions #这个包用来规避被检测的风险
import time #延迟
from chaojiying import Chaojiying_Client
from PIL import Image #处理图片
from selenium.webdriver import ActionChains #动作链
#这两段代码规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
driver_path=r'C:\Users\哥斯拉\AppData\Local\Google\Chrome\Application\chromedriver.exe' #定义好路径
driver=webdriver.Chrome(executable_path=driver_path,options=option) #初始化路径+规避检测
def register():
# driver.maximize_window()
driver.get('https://sec.lagou.com/verify.html?e=2&f=https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=')
verification = driver.find_element_by_xpath('//a[@class="btn"]').click()
time.sleep(2)
#save_screenshot 就是将当前页面截图并保存
driver.save_screenshot('aa.png')
#确定验证码图片左上角和右下角坐标(裁剪的区域就确定)
code_img=driver.find_element_by_xpath('//div[@class="geetest_widget"]')
location=code_img.location #验证码图片左上角左边 x,y
size=code_img.size #验证码标签对应得到长和宽
#左上角和右下角坐标
rangle=(int(location['x']),int(location['y']),int(location['x'])+size['width'],int(location['y']+size['height']))
#至此验证码区域确定下来
i=Image.open('./aa.png')# 打开刚刚的截图
code_img_name='./code.png'
#crop根据指定区域裁剪
frame=i.crop(rangle)
frame.save(code_img_name)
#将验证码传给超级鹰识别
chaojiying = Chaojiying_Client('xxxxxx', 'xxxxx', 'xxxxxx') # 超级鹰账号
im=open('code.png','rb').read()
print(chaojiying.PostPic(im,9008)['pic_str'])
result=chaojiying.PostPic(im,9008)['pic_str']
kk = driver.find_element_by_xpath('//div[@class="geetest_commit_tip"]')
all_list=[] #存储即将要点击点的坐标
global x
if '|' in result:
list_1=result.split('|')
count_1=len(list_1)
for i in range(count_1):
xy_list=[]
x=int(list_1[i].split(',')[0])
y = int(list_1[i].split(',')[1])
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
else:
x = int(list_1[i].split(',')[0])
y = int(list_1[i].split(',')[1])
xy_list = []
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
#遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置点击操作
print(all_list)
for l in all_list:
x=l[0]
y=l[1]
ActionChains(driver).move_to_element_with_offset(code_img,x,y).click().perform() #(这里面传入的是参照物也就是验证码的位置,x,y(xy是坐标))
time.sleep(0.5)
kk.click()
if __name__ == '__main__':
register()
我想让程序先点击完验证的图片之后在运行点击确认按钮,出问题的代码主要是从这里面的KK.click超级鹰还没选完图片它就执行了点击确认按钮
ActionChains(driver).move_to_element_with_offset(code_img,x,y).click().perform() #(这里面传入的是参照物也就是验证码的位置,x,y(xy是坐标))
time.sleep(0.5)
kk.click()