模擬登錄qq空間:有iframe、無驗證碼
"""
selenium模擬登錄QQ空間:有iframe、無驗證碼
"""
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
if __name__ == '__main__':
u = '你的賬戶'
p = '你的密碼'
# 實例化谷歌瀏覽器對象
service = Service('./chromedriver')
bro = webdriver.Chrome(service=service)
# 請求頁面
url = 'https://qzone.qq.com'
bro.get(url=url)
# 定位到登錄的iframe
bro.switch_to.frame('login_frame')
# 使用動作鏈:from selenium.webdriver import ActionChains
# 實例化動作鏈對象
action = ActionChains(bro)
# 找到並點擊賬號密碼登錄
change_plogin = bro.find_element(by=By.XPATH, value='//a[@id="switcher_plogin"]')
action.click(change_plogin).perform()
# 找到賬號框
user_input = bro.find_element(by=By.XPATH, value='//input[@id="u"]')
user_input.send_keys(u)
# 找到密碼框
pwd_input = bro.find_element(by=By.XPATH, value='//input[@id="p"]')
pwd_input.send_keys(p)
# 找到登錄按鈕
login_button = bro.find_element(by=By.XPATH, value='//input[@id="login_button"]')
# 點擊登錄按鈕
action.click(login_button).perform()
# 釋放動作鏈
action.release().perform()
# 關閉瀏覽器對象
# bro.quit()
模擬登錄12306:無iframe、有滑動驗證碼、有特征識別
"""
模擬登錄12306:無iframe,滑動驗證碼,檢測selenium
"""
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import ChromeOptions # 實現規避檢測風險
if __name__ == '__main__':
# 你的用戶名密碼
username = '你的賬戶'
password = '你的密碼'
service = Service('./chromedriver')
chrome_options = ChromeOptions()
# 規避檢測
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 實例化瀏覽器
bro = webdriver.Chrome(service=service,options=chrome_options)
# 指定url
url = 'https://kyfw.12306.cn/otn/resources/login.html'
bro.get(url=url)
# 找到用戶框
user_input = bro.find_element(by=By.XPATH, value='//input[@id="J-userName"]')
# 傳入你的用戶名
user_input.send_keys(username)
# 找到密碼框
pwd_input = bro.find_element(by=By.XPATH, value='//input[@id="J-password"]')
# 傳入你的密碼
pwd_input.send_keys(password)
# 找到登錄按鈕/鏈接
a_login = bro.find_element(by=By.XPATH, value='//a[@id="J-login"]')
# 點擊 a_login.click()
a_login.click()
# 使用動作鏈:from selenium.webdriver import ActionChains
# 實例化動作鏈對象
action = ActionChains(bro)
# 等待兩秒,不然會找不到滑塊
time.sleep(2)
# 解決特征識別
script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});'
bro.execute_script(script)
# 這時候會出現一個滑動驗證碼
slid_span = bro.find_element(by=By.ID, value='nc_1_n1z')
# 按住並滑動340px
action.click_and_hold(slid_span).perform()
for i in range(10):
action.move_by_offset(34, 0).perform()
time.sleep(0.2)
action.release().perform()
# bro.quit()