chromeDriver selenium taobao

2022-02-07  本文已影响0人  手捧樱花v

目前会无限滑块,明天看看解决方案,不行Firefox

from tabnanny import check
import time
from numpy import printoptions
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class TB():
    def __init__(self,*args,**kwargs):
        option = webdriver.ChromeOptions()
        option.add_experimental_option("excludeSwitches", ["enable-automation"]) //屏蔽chrome正受到自动测试软件的控制
        self.webdriver_obj = webdriver.Chrome(executable_path="chromedriver",options=option)
        # self.webdriver_obj.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        #     "source": '''
        #     Object.defineProperty(navigator, 'webdriver', {
        #       get: () => undefined
        #     })
        #   '''
        # })

    def xpath(self,xpath_base): //校验Xpath是否获取失败
        print(xpath_base)
        try:
            element = WebDriverWait(self.webdriver_obj,10).until(EC.presence_of_element_located((By.XPATH,xpath_base)))
        except:
            element = []
        finally:
            return element

    def login(self,username='',password=''):
        print("login")
        url = 'https://login.taobao.com/member/login.jhtml'
        self.webdriver_obj.get(url)
        self.xpath('//*[@id="fm-login-id"]').send_keys(username)  
        self.xpath('//*[@id="fm-login-password"]').send_keys(password) 
        # self.xpath('//*[@id="login-form"]/div[4]/button').click() 
        self.webdriver_obj.switch_to.frame(self.xpath('//*[@id="baxia-dialog-content"]')) //验证码滑块存在iframe中
        check_yzm_span = self.xpath('//*[@id="nc_1_n1z"]')
        if check_yzm_span:
            print("yzm_exist")
            self.check_yzm(check_yzm_span)
        else:
            print("no yzm")
        print("done")

    def check_yzm(self,check_yzm_span):
        action = ActionChains(self.webdriver_obj)
        action.click_and_hold(check_yzm_span).perform()
        action.move_by_offset(xoffset=260,yoffset=0).perform() //260=滚动条长度300-滑块42,稍微大于258
        self.webdriver_obj.switch_to.default_content() //退出滑块的iframe
        self.xpath('//*[@id="login-form"]/div[4]/button').click()

if __name__ == '__main__':
    start = TB()
    start_login = start.login("mobile","pwd")

上一篇 下一篇

猜你喜欢

热点阅读