我想让电脑帮忙点按钮——07

1——找到目标的位置

2——Python实现

# author: mmm
# datetime:2022/6/25 23:10周六
# software: PyCharm
"""
文件说明:
"""
# author: mmm
# datetime:2022/6/25 14:30周六
# software: PyCharm
"""
文件说明:
"""
import requests
from lxml import etree
import pandas as pd
import json
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from PIL import Image
ua = {
    "user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
      }
# 1. 新建浏览器对象driver
driver = webdriver.Chrome()
driver.maximize_window()
# 1. 打开第一个页面
driver.get('https://www.douban.com/')
d1 = driver.page_source  # 1.1 获取当前页面源代码
cook = driver.get_cookies()  # 1.2 获取cookie值
url = driver.current_url  # 1.3 获取当前的url
cn = driver.window_handles  # 1.4 获取当前打开了几个页面
# 1.4.1 打开多个页面
driver.execute_script("window.open('https://www.163.com')")
driver.execute_script("window.open('https://sz.meituan.com/')")
driver.switch_to_window(cn[0])  # 1.5 切换页面
driver.back()
time.sleep(0.3)
driver.forward()
time.sleep(0.04)
# 1.6 cookie传值给requests
# data = requests.get(driver.current_url, cookies={}, headers=ua).text()
# 1.7 使用xpath获取数据
# data1 = etree.HTML(data).xpath('')
# 1.8 发送请求——>获取数据——>json解析....
# 1.9 使用代理 快代理(免费不稳定)
proxies = {
    # http代理只能使用Http协议的网站
    'http': 'http://58.20.235.180:9091',
    'http': 'http://112.6.117.135:8085',
    'http': 'http://58.20.184.187:9091',
    'http': 'http://58.20.235.180:9091',
    'http': 'http://223.82.60.202:8060'
    }
# 2. 定位到iframe子页面
ele1 = driver.find_element_by_tag_name('iframe')
# 3. 切换到iframe子页面
driver.switch_to.frame(ele1)
# 4. xpath定位到元素
element1 = driver.find_element_by_xpath('//li[text()="密码登录"]')
element1.click()
# 4.1 定位到用户名和密码
# username = driver.find_element_by_xpath('//*[@id="username"]')
username = driver.find_element_by_id("username")
username.clear()
username.send_keys('209695280@qq.com')
passwd = driver.find_element_by_xpath('//*[@id="password"]')
passwd.clear()
passwd.send_keys('zhangsan')
# 5. 点击登录
login = driver.find_element_by_xpath('//a[text()="登录豆瓣"]')
login.click()
# 6. 模拟隔几秒,关闭窗口
time.sleep(3)
# 7. 当前页面截图
# driver.save_screenshot('12345.png')
# 8. xpath获取验证码的图片
# 8.1 定位到验证码图片的xpath
img1 = driver.find_element_by_class_name('xxx')
# 8.2 获取验证码图片的左上角坐标
x_y = img1.location
# 8.3 获取验证码图片的宽和高
w_h = img1.size
# 8.4 获得右下角的坐标
cor_xy = (x_y['x'], x_y['y'], x_y['x'] + w_h['width'], x_y['y'] + w_h['height'])
# 9. 使用超级鹰[工具]来抠图
koutu = Image.open('12345.png').crop(cor_xy)
koutu.save('c:/vali_code.png')
img2 = open('c:/vali_code.png', 'rb').read()
# 9.2 获得抠图的坐标 x1,y1|x2,y2|x3,y3....
cors_xy = chaojiying.PostPic(img2, 9004)['pic_str']
if "|" in cors_xy:
    a_list = [[  int(i.split(',')[0]), int(i.split(',')[1])]  for i in cors_xy.split('|')]
else:
    a_list = [[int(cors_xy.split(',')[0]), int(cors_xy.split(',')[1])]]
for a in a_list:
    xa = a[0]
    ya = a[1]
    # 9.3 鼠标移动到坐标的位置
    ActionChains(driver).move_to_element_with_offset(img1, xa, ya).click().perform()
# 10. over,退出
driver.quit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值