1. 程式人生 > >selenium chrome登陸手機 pc淘寶

selenium chrome登陸手機 pc淘寶

mobile rime 觸發 drive rom googl turn cookies driver

接口登錄淘寶,困難度極高,沒有人已經實現過。

淘寶登錄selenium 手機版 pc版。

由於每天需要使用ip代理大批量的異地登錄淘寶帳號,這種情況必然會出現淘寶滑動驗證碼,使用ActionChains,

使用手機版m.taobao.login登錄,采用短信驗證碼交互方式登錄,獲取驗證碼按鈕的點擊需要使用TouchActions,不能通過click觸發。

但魔蠍科技app也沒有提供淘寶在後臺處理登錄。

#coding=utf8
import platform,time,random,os


from selenium import webdriver
from selenium.webdriver.support.wait import
WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.touch_actions import
TouchActions from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options import config #print os.getenv(‘PATH‘) class BrowserUtil(object): def __init__(self, is_mobile=0): self.chrome_options = Options() self.cookies_dict = {} self.set_useragent(is_mobile)
def __del__(self): pass self.driver.quit() def set_options(self,headless=1,display_pictures=0): # self.chrome_options.binary_location = ‘/opt/google/chrome/chrome‘ self.chrome_options.add_argument(lang=zh_CN.UTF-8) if headless == 1: self.chrome_options.add_argument(--headless) self.chrome_options.add_argument(--disable-gpu) if not display_pictures: prefs = {"profile.managed_default_content_settings.images": 2} self.chrome_options.add_experimental_option("prefs", prefs) def set_proxy(self): self.chrome_options.add_argument(--proxy-server=http:// + 112.85.84.154:1131) def set_useragent(self,is_mobile): if not is_mobile: self.chrome_options.add_argument(user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36") else: self.chrome_options.add_argument(user-agent="Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Mobile Safari/537.36") mobileEmulation = {deviceName: Nexus 6P} self.chrome_options.add_experimental_option(mobileEmulation, mobileEmulation) def setup_browser(self,maxsize=1): self.driver = webdriver.Chrome(chrome_options=self.chrome_options) if maxsize: self.driver.maximize_window() self.driver.set_page_load_timeout(60) self.driver.implicitly_wait(30) def touch_element(self,element): TouchActions(self.driver).tap(element).perform() def get_cookies_dict(self): driver_cookie = self.driver.get_cookies() for c_dict in driver_cookie: self.cookies_dict[c_dict[name]] = c_dict[value] def delete_all_cookies(self): self.driver.delete_all_cookies() def add_cookie(self,cookie_dict): """ :type cookie_dict :dict """ for k,v in cookie_dict.items(): cookie = {name:k , value:v} self.driver.add_cookie(cookie) def wait_element_by_id(self, wait_time, element_id): WebDriverWait(self.driver, wait_time, 0.5).until(EC.presence_of_element_located((By.ID, element_id))) def click_element_by_id(self,element_id): self.driver.find_element_by_id(element_id).click() def get(self,url): self.driver.get(url) def save_screen(self,filename): if platform.system() == Windows: pic_path = config.screen_pic_windows else: pic_path = config.screen_pic_linux if not os.path.exists(pic_path): os.makedirs(pic_path) filenamex = pic_path + filename self.driver.save_screenshot(filenamex) def get_track(self,distance): track = [ ] current = 0 mid = distance *4 / 5 t = 0.2 v = 0 while current < distance: if current < mid: a = 2 else: a = -3 v0 = v v = v0 + a * t move = v0 * t + 1 / 2 * a * t * t current += move track.append(round(move)) return track def drag_element(self,element): tracks = self.get_track(500) action = ActionChains(self.driver) action.move_to_element(element).perform() action.click_and_hold(on_element=element).perform() for x in tracks: yoffset = random.randint(-10, 10) time_sleep = random.randint(1,2)/10.0 time_sleep = random.randint(50,100)/1000.0 time.sleep(time_sleep) action.move_to_element_with_offset(to_element=element, xoffset=x,yoffset=yoffset).perform() #ActionChains(self.driver).drag_and_drop_by_offset(element,500,yoffset).perform() action.release(on_element=element).perform()

selenium chrome登陸手機 pc淘寶