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登陆并点击百度平台的更多相关文章

  1. python+selenium+chrome实现自动登录百度

    #python3.4+selenium3.5+chrome版本 63.0.3239.132+chrome驱动chromedriver.exe #实现自动登录百度 from selenium impor ...

  2. python selenium登陆网易云音乐

    from selenium import webdriver import time driver=webdriver.Chrome() driver.get("http://music.1 ...

  3. python selenium 自动化测试web

    如何使用python完成自动化测试web页面呢?首选selenium   那基于python的selenium如何使用,下面看一段测试案例: 基于python的selenium 安装方法: pip i ...

  4. python+selenium+unnittest框架

    python+selenium+unnittest框架,以百度搜索为例,做了一个简单的框架,先看一下整个项目目录结构 我用的是pycharm工具,我觉得这个工具是天使,超好用也超好看! 这些要感谢原作 ...

  5. 学霸笔记系列 - Python Selenium项目实战(一)—— 怎么去验证一个按钮是启用的(可点击)?

    Q: 使用 Python Selenium WebDriver 怎么去验证一个按钮是启用的(可点击)? A:Selenium WebDriver API 里面给出了解决方法is_enabled() 使 ...

  6. python selenium自动化点击页面链接测试

    python selenium自动化点击页面链接测试 需求:现在有一个网站的页面,我希望用python自动化的测试点击这个页面上所有的在本窗口跳转,并且是本站内的链接,前往到链接页面之后在通过后退返回 ...

  7. Python+Selenium自动化-清空输入框、输入内容、点击按钮

    Python+Selenium自动化-清空输入框.输入内容.点击按钮   1.输入内容 send_keys('valve'):输入内容valve #定位输入框 input_box = browser. ...

  8. python+selenium+unnitest写一个完整的登陆的验证

    import unittest from selenium import webdriver from time import sleep class lonInTest (unittest.Test ...

  9. Python+Selenium基础入门及实践

    Python+Selenium基础入门及实践 32018.08.29 11:21:52字数 3220阅读 23422 一.Selenium+Python环境搭建及配置 1.1 selenium 介绍 ...

随机推荐

  1. Spring MyBatis配置文件

    Config文件夹 log4j.properties配置文件 # # Global logging configuration log4j.rootLogger=DEBUG, stdout # Con ...

  2. switch下返回各类的数值

    定义一个变量,在每个case下赋值,最后return public static int orderDishes(int choice) { int price = 0; switch (choice ...

  3. CF1353E K-periodic Garland(贪心/dp)

    Question 有n盏灯,0代表暗,1代表亮,相邻两个1之间为周期k,求出最少的改变次数 Solution First 贪心方法 详见博客https://blog.csdn.net/cheng__y ...

  4. [JavaWeb基础] 012.Struts2 自定义标签使用

    在做开发中,我们会把一些比较经常使用到的代码封装起来,这样可以加快开发的速度和减少错误,并且在修改bug可以一次修改多次修复.那么在前端页面上,如果我们要经常用到公用的显示功能,并涉及到服务端逻辑操作 ...

  5. Linux实验

    实验一 Linux系统安装与简单配置 一.实验目的 1.掌握Linux系统安装的分区准备. 2.掌握Linux系统的安装步骤. 3.掌握Linux系统分区的挂载和卸载. 4.掌握Linux系统的启动和 ...

  6. win10系统下计算器界面变成英文的解决方法

    标题: win10系统下计算器界面变成英文的解决方法 作者: 梦幻之心星 347369787@QQ.com 标签: [win10, 计算器, 英文] 目录: 软件 日期: 2019-04-20 目录 ...

  7. Java实现 LeetCode 659 分割数组为连续子序列 (哈希)

    659. 分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [ ...

  8. Java实现 LeetCode 553 最优除法(思路问题)

    553. 最优除法 给定一组正整数,相邻的整数之间将会进行浮点除法操作.例如, [2,3,4] -> 2 / 3 / 4 . 但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级.你需要 ...

  9. Java实现字符串编辑距离

    1 问题描述 给定一个源串和目标串,能够进行如下操作: 在任意位置上插入一个字符: 替换掉任意字符: 删除任意字符. 写一个程序,实现返回最小操作次数,使得对源串进行上述这些操作后等于目标串. 2 解 ...

  10. 【CSS】电脑、移动端公用样式

    电脑端: /* Public */ @charset "utf-8"; html, body, div, p, ul, ol, li, dl, dt, dd, h1, h2, h3 ...