import threading
import time
import warnings
from selenium.webdriver import DesiredCapabilities, ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as options
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
def WebDriverChrome():
new_driver_path = r"D:\Program Files\Chrome\Application\chromedriver.exe"
new_binary_path = r"D:\Program Files\Chrome\Application\chrome.exe"
ops = options()
ops.binary_location = new_binary_path
serv = Service(new_driver_path)
caps = DesiredCapabilities.CHROME # 修改页面加载策略
#normal(整页加载) eager(交互式) none
caps["pageLoadStrategy"] = "normal" # 注释这两行会导致最后输出结果的延迟,即等待页面加载完成再输出
Options = webdriver.ChromeOptions()
#Options.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])#屏蔽自动化受控提示 && 开发者提示
Options.add_argument('--start-maximized') # 最大化运行(全屏窗口),不设置,取元素会报错
Options.add_argument('--no-sandbox') #解决DevToolsActivePort文件不存在的报错
Options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
Options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
Options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
#Options.add_argument('--incognito') # 无痕模式
Options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
driver = webdriver.Chrome(desired_capabilities=caps,service=serv, options=Options)
#过检测
with open('stealth.min.js') as f:
js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": js})
return driver
def browser_quit(browser):
browser.close()
browser.quit()
def login_taobao(username,password):
browser = WebDriverChrome()
url = "https://login.taobao.com/member/login.jhtml"
browser.get(url)
#智能等待,直到网页加载完毕,最长等待时间为30s
#browser.implicitly_wait(3)
#输入账户 fm-login-id
fm_login_id = browser.find_elements(By.NAME,'fm-login-id')
if len(fm_login_id) == 0:
browser.refresh()
browser.implicitly_wait(3)
browser.find_element(By.NAME,'fm-login-id').send_keys(username)
#输入密码 fm-login-password
browser.find_element(By.NAME,'fm-login-password').send_keys(password)
time.sleep(1)
ele = browser.find_elements(By.ID,'baxia-dialog-content')
#有滑块处理
if len(ele) != 0:
browser.switch_to.frame('baxia-dialog-content')
#baxia-dialog-content
huakuai = browser.find_element(By.ID,'nc_1_n1z')
box = browser.find_element(By.ID,'nc_1__scale_text')
print('过滑块')
ActionChains(browser).drag_and_drop_by_offset(huakuai,box.size['width'],0).perform()
#释放iframe
browser.switch_to.default_content()
#已阅读并同意 fm-agreement-checkbox
fm_agreement_checkbox = browser.find_elements(By.CLASS_NAME,'fm-agreement')
if len(fm_agreement_checkbox) != 0:
#browser.find_element(By.CLASS_NAME,'fm-agreement').click()
fm_agreement_checkbox[0].click()
#点击登录按钮 fm-btn
fm_btn = browser.find_elements(By.XPATH,'//*[@id="login-form"]/div[4]/button')
if len(fm_btn) != 0:
if fm_btn[0].get_dom_attribute("class").find('fm-button-disabled') != -1:
warnings.warn("登录按钮被禁用")
return None
else:
fm_btn[0].click()
else:
warnings.warn("无登录按钮")
return None
#检测URL是否已经变化,变化我就认为登录成功,简单点嘛
print('准备取COOKIE')
time.sleep(2)
while True:
if browser.current_url != url:
break;
time.sleep(0.1)
#cookie取到了
cookie = "; ".join([item["name"] + "=" + item["value"] for item in browser.get_cookies()])
#关闭浏览器
threading.Thread(target=browser_quit, args=([browser])).start()
#打印cookie
return cookie
username = ''
password = ''
print(login_taobao(username,password))
|