selenium使用

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import time
# 解决自动化控制问题防止被检测到
option=Options()
option.add_argument('--disable-blink-features=AutomationControlled')
web=Chrome(options=option)

# web=Chrome()
web.get('https://kyfw.12306.cn/otn/resources/login.html')
time.sleep(3)
web.find_element(By.XPATH,'//*[@id="J-userName"]').send_keys('12345678')
time.sleep(2)
web.find_element(By.XPATH,'//*[@id="J-password"]').send_keys('1223455555')
time.sleep(2)
web.find_element(By.XPATH,'//*[@id="J-login"]').click()
x=1
y=1
# 以某个为基点移动多少
# ActionChains(web).move_to_element_with_offset(,x,y).clock().perform()点击提交
#

# 拖拽验证码
time.sleep(5)
btn=web.find_element(By.XPATH,'/html/body/div[1]/div[4]/div[2]/div[2]/div/div/div[2]/div/div[1]/span')
# 拖拽

ActionChains(web).drag_and_drop_by_offset(btn,300,0).perform()

切换窗口:

# 切换窗口到达视频播放页面
web.switch_to.window(web.window_handles[-1])

通过标签名寻找:

web.find_element(By.TAG_NAME,'button').click()

无头操作:

    # 无头操作
    opt=Options()
    opt.add_argument("--headless")
    opt.add_argument('--disbale-gpu')
    # 创建浏览器对象
    web=Chrome(options=opt)

超级鹰处理验证码

# 找到验证码图片
img=web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys('账号')
time.sleep(3)
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys('密码')
chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '1111固定的')

dic=chaojiying.PostPic(img, 1902)
version=dic['pic_str']
time.sleep(1)
# 输入验证码
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(version)
time.sleep(3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值