Loading

2-模拟登录淘宝

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])  # 这里去掉window.navigator.webdriver的特性
option.add_argument("--disable-blink-features=AutomationControlled")   # 屏蔽webdriver特征
browser = webdriver.Chrome(options=option)
browser.get('https://www.taobao.com/')
browser.maximize_window()
button = WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.CLASS_NAME, 'h')))
button.click()
# username_sender = WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.ID, 'fm-login-id')))
username_sender = browser.find_element_by_xpath('//input[@id="fm-login-id"]')
username_sender.send_keys("user_name")
# password_sender = WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.ID, 'fm-login-password')))
password_sender = browser.find_element_by_id('fm-login-password')
password_sender.send_keys("password")
sleep(3)
try:
    browser.switch_to.frame(0)
    # 找到滑块
    slider = browser.find_element_by_xpath("//span[contains(@class, 'btn_slide')]")
    # 判断滑块是否可见
    if slider.is_displayed():
        # 点击并且不松开鼠标
        ActionChains(browser).click_and_hold(on_element=slider).perform()
        # 往右边移动258个位置
        ActionChains(browser).move_by_offset(xoffset=258, yoffset=0).perform()
        # 松开鼠标
        ActionChains(browser).pause(0.5).release().perform()
        browser.switch_to.default_content()
except:
    pass
button = WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.CLASS_NAME, 'password-login')))
button.click()
posted @ 2021-08-28 16:28  lotuslaw  阅读(98)  评论(0编辑  收藏  举报