通过popen直接运行chrome执行文件,通过参数--remote-debugging-port打开调试端口,selenium通过上面打开的调试端口链接到chrome
class TaoBao():
def __init__(self,taskid,proxy=None,port=None,init=False):
self.init=init
self.taskid=taskid
self.screenCount=1
self.login_url='https://loginmyseller.taobao.com/'
self.headers = {"Origin": "https://myseller.taobao.com",
"Upgrade-Insecure-Requests": "1",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Referer": "https://myseller.taobao.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"User-Agent": user_agent}
# from pywinauto.application import Application
# Application().start(
# chrome_path + " --start-maximized --remote-debugging-port=%s --user-data-dir=%s" % (port, chrome_info))
if port:
chrome_port=port
chrome_info=config.get_common('chrome_info')+str(port)
else:
chrome_port = config.get_common('chrome_port')
chrome_info = config.get_common('chrome_info')
if not init:
os.popen('sudo chmod -R 777 '+chrome_info)
if proxy:
os.popen(chrome_path + " --start-maximized --remote-debugging-port=%s --user-data-dir=%s --proxy-server=%s --proxy-user-name=%s --proxy-password=%s" % (chrome_port, chrome_info,proxy.get('url'),proxy.get('user'),proxy.get('passwd')))
else:
os.popen(
chrome_path + " --start-maximized --remote-debugging-port=%s --user-data-dir=%s" % (
chrome_port, chrome_info))
time.sleep(1)
options = webdriver.ChromeOptions()
options.debugger_address = "127.0.0.1:%s" % chrome_port
options.binary_location = chrome_path # 有多个浏览器时设置路径
options.add_argument("user-data-dir=%s" % chrome_info) # 使用自己设置的浏览器信息
self.browser = webdriver.Chrome(executable_path=chrome_driver, options=options)
#yin shi deng dai
self.browser.implicitly_wait(5)
#获取浏览器边缘像素
self.panel_height = self.browser.execute_script('return window.outerHeight - window.innerHeight')
# 输入网址
pyautogui.press('shift')
pyautogui.typewrite(self.login_url, 0.1)
pyautogui.press('enter')
if proxy:
time.sleep(1)
pyautogui.typewrite(proxy.get('user'))
pyautogui.press('tab')
pyautogui.typewrite(proxy.get('passwd'))
pyautogui.press('enter')