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. nginx: [error] invalid PID number "" in ...

    1.查看进程 ps -ef|grep nginx 2.进入nginx安装目录sbin下,执行命令: ./nginx -t 如下显示: syntax is ok test is successful 3 ...

  2. 使用VuePress搭建个人博客

    使用VuePress搭建个人博客 VuePress 是一个基于 Vue 的静态网站生成器.其中主要用到:Vue,VueRouter,Webpack. 类似的工具:hexo 基于 Markdown 语法 ...

  3. neo4j-jdbc driver

    https://github.com/neo4j-contrib/neo4j-jdbc/releases/tag/3.3.1

  4. 树莓派 Ubuntu Mate更换中国软件源

    更换步骤: 1.以root身份打开 /etc/apt/sources.list ,可以用vim或者nano 2.将 http://ports.ubuntu.com/ 全部替换为 http://mirr ...

  5. 【极客思考】计算机网络:Wireshark抓包分析TCP中的三次握手与四次挥手

    [摘要]本文重点分析计算机网络中TCP协议中的握手和挥手的过程. [前提说明] 前段时间突然看到了一篇关于TCP/IP模型的文章,心想这段时间在家里也用wireshark抓了点包,那么想着想着就觉得需 ...

  6. Shell 脚本(四) ”read 读取控制台输入” 与 “函数”

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 八.read 读取控制台输入 1.基本语法 read(选项)(参数) 选项: -p: 指定读取值时的提示 ...

  7. Redis 入门到分布式 (六)常见的持久化开发运维问题

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.常见问题目录 fork操作 进程外开销 AOF追加阻塞 单机多实例部署 二. fork 1.Fork ...

  8. Jquery拓展方法

    拓展JQuery一般有两个方法: 1.全局拓展 比如 $.ajax()就是全局函数 拓展方法: Jquery.extend(); 比如: $.extend({ hello:function(mynam ...

  9. Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)

    542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...

  10. Java实现 LeetCode 413 等差数列划分

    413. 等差数列划分 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为等差数列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, - ...