python selenium滑块验证码登录牛客网

# -*- coding:utf8 -*-
import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains


class NowCoderLogin(object):

    # 规避识别
    option = ChromeOptions()
    browser = webdriver.Chrome(options=option)  # 声明一个浏览器对象
    option.add_experimental_option('excludeSwitches', ['enable-automation'])

    # 打开页面优先执行的js,execute_cdp_cmd
    browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
            Object.defineProperty(navigator, 'webdriver', {
              get: () => undefined
            })
          """
    })

    def open_nowcoder_login_page(self):

        self.browser.get('https://www.nowcoder.com/')  # 打开Chrome
        _click_login = WebDriverWait(self.browser, 5, 0.5).until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div/header/nav/div[2]/button/span/a[1]'))
        )
        _click_login.click()

        time.sleep(1)

        _click_password_login = WebDriverWait(self.browser, 5, 0.5).until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div/div[1]/div/div[2]/div[2]/div[1]/ul/li[3]'))
        )
        _click_password_login.click()

        time.sleep(1)

        login = WebDriverWait(self.browser, 5, 0.5).until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div/div[1]/div/div[2]/div[2]/form/div[1]/div/div[1]/input'))
        )
        login.send_keys('15532634791')

        time.sleep(1)

        password = WebDriverWait(self.browser, 5, 0.5).until(
            EC.presence_of_element_located(
                (By.XPATH, '/html/body/div/div[1]/div/div[2]/div[2]/form/div[2]/div/div/input'))
        )
        password.send_keys('密码')

        time.sleep(1)

        button = WebDriverWait(self.browser, 5, 0.5).until(
            EC.presence_of_element_located(
                (By.XPATH, '/html/body/div/div[1]/div/div[2]/div[2]/form/button'))
        )
        button.click()

        time.sleep(2)

        while True:

            element = self.browser.find_element(By.CLASS_NAME,'yidun_slider')
            print(element)

            #     鼠标点击元素并按住不放
            print("第一步,点击元素")

            ActionChains(self.browser).click_and_hold(on_element=element).perform()
            time.sleep(1)

            print("第二步,拖动元素")

            #     拖动鼠标到指定的位置,注意这里位置是相对于元素左上角的相对值
            ActionChains(self.browser).move_to_element_with_offset(to_element=element, xoffset=145, yoffset=0).perform()
            time.sleep(1)

            print("第三步,释放鼠标")

            #     释放鼠标
            ActionChains(self.browser).release(on_element=element).perform()

            time.sleep(2)
        # self.browser.quit()


if __name__ == '__main__':
    now_coder = NowCoderLogin()
    now_coder.open_nowcoder_login_page()



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值