# -*- 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()
python selenium滑块验证码登录牛客网
于 2022-02-11 15:40:30 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)