|
楼主 |
发表于 2020-8-13 14:41:00
|
显示全部楼层
from selenium import webdriver
import time
class loginclass:
def __init__(this):
# chrome_options 初始化选项
this.chrome_options = webdriver.ChromeOptions()
this.chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
# 不加载图片,加快访问速度
# this.chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
# 关闭开发者模式
this.chrome_options.add_experimental_option("useAutomationExtension", False)
# 设置为开发者模式,避免被识别
# this.chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
this.web_driver = webdriver.Chrome(r"D:\Users\Liht\PycharmProjects\taobao\taobao\chromedriver.exe",options=this.chrome_options)
# this.web_driver_wait = WebDriverWait(this.web_driver, 10 , 0.5)
# 通过浏览器的dev_tool在get页面钱将.webdriver属性改为"undefined"
this.web_driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""",
})
this.url = 'https://login.taobao.com/member/login.jhtml'
this.username = "."
this.password = "."
def get_login_cookie(this):
# this.chrome_options = webdriver.ChromeOptions()
# this.chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
# this.chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
# # 关闭开发者模式
# this.chrome_options.add_experimental_option("useAutomationExtension", False)
# browser = webdriver.Chrome(r"D:\Users\Liht\PycharmProjects\taobao\taobao\chromedriver.exe", options=this.chrome_options)
# # 通过浏览器的dev_tool在get页面钱将.webdriver属性改为"undefined"
# browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
# "source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""",
# })
this.web_driver.get(this.url)
# browser.maximize_window() # 最大化
# 填写用户名密码
id = this.username
password = this.password
this.web_driver.find_element_by_xpath('//*[@id="fm-login-id"]').clear()
this.web_driver.find_element_by_xpath('//*[@id="fm-login-id"]').send_keys(id)
this.web_driver.find_element_by_xpath('//*[@id="fm-login-password"]').clear()
this.web_driver.find_element_by_xpath('//*[@id="fm-login-password"]').send_keys(password)
time.sleep(2)
# 登录 //*[@id="J_GetCode"]
this.web_driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
# print('登录成功\n')
if this.is_element_exist("#J_GetCode"):
this.web_driver.find_element_by_xpath('//*[@id="J_GetCode"]').chick()
yzm = input("请输入手机验证码:")
this.web_driver.find_element_by_xpath('//*[@id="J_Phone_Checkcode"]').send_keys(yzm)
this.web_driver.find_element_by_xpath('//*[@id="submitBtn"]').chick()
#登录后弹框=》跳过
# if this.is_element_exist("/html/body/div[5]/div/div[2]/div[3]/div[2]"):
this.web_driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[3]/div[2]').chick()
if this.is_element_exist("#container"):
this.web_driver.find_element_by_xpath('//*[@id="container"]/div/div[2]/div[1]/div/div/div[3]/div[1]/ul/li[1]/a').click()
input("禁止跳出")
def is_element_exist(this,css):
s = this.web_driver.find_elements_by_css_selector(css_selector=css)
if len(s) == 0:
print("元素未找到:%s"%css )
return False
elif len(s) == 1:
return True
else:
print("找到%s个元素:%s"%(len(s),css))
return False
if __name__ == '__main__':
loginclass().get_login_cookie() |
|