python + selenium登陆并点击百度平台

2021-02-09 13:20

阅读:448

标签:sele   pytho   img   website   end   __name__   ==   百度   common   

from PIL import Image
from selenium.webdriver import DesiredCapabilities
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


class MsspBqt(object):
def __init__(self):
self.url = ‘https://mssp.baidu.com/bqt#/‘
self.opt = webdriver.ChromeOptions()
capa = DesiredCapabilities.CHROME
capa[‘pageLoadStrategy‘] = ‘none‘ # 懒加载模式,不等待页面加载完毕
# self.opt.set_headless()
# 设置随机请求头
self.opt.add_argument(‘User-Agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0‘)
self.driver = webdriver.Chrome(desired_capabilities=capa)
self.driver.maximize_window()
self.wait = WebDriverWait(self.driver, 20)

def login(self):
self.driver.get(self.url)
# 等待登陆按钮元素加载出来
self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, ‘btn-login‘)))
# 点击登陆按钮
self.driver.find_element_by_class_name(‘btn-login‘).click()
time.sleep(0.2)
# 输入账号密码和验证码
self.driver.find_element_by_id(‘username‘).send_keys(‘账号‘)
self.driver.find_element_by_id(‘password‘).send_keys(‘密码‘)
self.getCaptcha()
captcha = input("请输入验证码:")
self.driver.find_element_by_id(‘imagecode‘).send_keys(captcha)
self.driver.find_element_by_xpath(‘//div[@class="login-form-btn login-form-group"]/button‘).click()
time.sleep(5)
self.creat()

def creat(self):
for i in range(31):
if i == 0:
continue
self.driver.get(‘https://mssp.baidu.com/bqt/websiteco.html#/union/slot/create‘)
time.sleep(3)
self.wait.until(EC.presence_of_element_located((By.XPATH, ‘//section/div/div/label/input[@class="veui-input-input"]‘)))
self.driver.execute_script(‘window.stop();‘) # 停止当前页面加载,防止input框输入错误
self.driver.find_element_by_xpath(‘//section/div/div/label/input[@class="veui-input-input"]‘).clear()
self.driver.find_element_by_xpath(‘//section/div/div/label/input[@class="veui-input-input"]‘).send_keys(‘---‘)
# 投放平台点击PC
self.driver.find_element_by_xpath(‘//section[2]/div/div/button[@class="veui-button"]‘).click()
if i % 2:
print(‘奇数:‘, i)
self.driver.find_element_by_xpath(
‘//form/div[@class="form-operation"]/button[@class="veui-button"][1]‘).click()
else:
print(‘偶数:‘, i)
# 图文广告
self.driver.find_element_by_xpath(‘//section[3]/div/div/button[@class="veui-button"]‘).click()
# 确定按钮
self.driver.find_element_by_xpath(‘//form/div[@class="form-operation"]/button[@class="veui-button"][1]‘).click()
# 等待新建代码位按钮加载出来
self.wait.until(EC.presence_of_element_located((By.XPATH, ‘//form/div/button[@class="veui-button"]‘)))

def getCaptcha(self):
self.driver.save_screenshot(‘current_page.png‘)
# 验证码的坐标位置
left = 1876
top = 271
right = 1970
bottom = 326
img = Image.open(‘current_page.png‘)
img = img.crop((left, top, right, bottom))
img.save(‘captcha.png‘)


if __name__ == "__main__":
bqt = MsspBqt()
bqt.login()


原文链接:https://blog.csdn.net/qq_41866851/java/article/details/100585564

python + selenium登陆并点击百度平台

标签:sele   pytho   img   website   end   __name__   ==   百度   common   

原文地址:https://www.cnblogs.com/modentime/p/12749651.html


评论


亲,登录后才可以留言!