python + selenium登陆并点击百度平台
Posted modentime
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python + selenium登陆并点击百度平台相关的知识,希望对你有一定的参考价值。
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(‘---<‘+str(i)+‘>‘)
# 投放平台点击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登陆并点击百度平台的主要内容,如果未能解决你的问题,请参考以下文章
九Python+Selenium模拟用QQ登陆腾讯课堂,并提取报名课程(练习)
python selenium百度搜索,怎么写才能点击该链接查看?