126 lines
3.5 KiB
Python
126 lines
3.5 KiB
Python
import os
|
|
import time
|
|
from selenium import webdriver
|
|
from selenium.webdriver.chrome.options import Options
|
|
from selenium.webdriver.chrome.service import Service
|
|
from selenium.webdriver.common.by import By
|
|
from selenium.webdriver.common.keys import Keys
|
|
from selenium.webdriver.common.action_chains import ActionChains
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
|
|
__driver__ = webdriver.Chrome
|
|
globals().__setitem__('__driver__', getattr(webdriver, os.environ.get('BROWSER_CHOOSE') or 'Chrome'))
|
|
|
|
|
|
class Browser(__driver__):
|
|
def __init__(self, driver=None, binary=None, home=None):
|
|
service = Service()
|
|
options = Options()
|
|
if driver:
|
|
service.path = driver
|
|
else:
|
|
service = None
|
|
if binary:
|
|
options.binary_location = binary
|
|
else:
|
|
options = None
|
|
super().__init__(service=service, options=options)
|
|
self.maximize()
|
|
self.implicitly_wait(10)
|
|
home and self.turn(home)
|
|
|
|
@staticmethod
|
|
def wait(sec):
|
|
"""
|
|
强制等待,单位:秒;
|
|
"""
|
|
time.sleep(sec)
|
|
|
|
def turn(self, url=None):
|
|
"""
|
|
跳转新的网址;
|
|
:param url:
|
|
:return:
|
|
"""
|
|
return self.get(url)
|
|
|
|
def find(self, value):
|
|
return self.find_element(By.XPATH, value)
|
|
|
|
def tab_create(self, url=None):
|
|
"""
|
|
打开新的标签页并切换至新标签页;
|
|
"""
|
|
self.switch_to.new_window('tab')
|
|
isinstance(url, str) and self.turn(url)
|
|
|
|
def tab_close(self):
|
|
"""
|
|
关闭当前标签页并切换至前一个标签页;
|
|
"""
|
|
handles = self.window_handles
|
|
if len(handles):
|
|
current = handles.index(self.current_window_handle)
|
|
self.close()
|
|
current > 0 and self.switch_to.window(handles[current-1])
|
|
|
|
def tab_switch(self, tab):
|
|
"""
|
|
Index 按索引切换标签;
|
|
Previous 切换上一个标签;
|
|
Next 切换下一个标签;
|
|
Start 切换到起始标签;
|
|
End 切换到末尾标签;
|
|
"""
|
|
handles = self.window_handles
|
|
lengths = len(handles)
|
|
current = handles.index(self.current_window_handle)
|
|
if isinstance(tab, int):
|
|
handle = tab
|
|
elif tab == 'Previous':
|
|
handle = (current - 1)
|
|
elif tab == 'Next':
|
|
handle = (current + 1) % lengths
|
|
elif tab == 'Start':
|
|
handle = 0
|
|
elif tab == 'End':
|
|
handle = 0-1
|
|
else:
|
|
handle = None
|
|
self.switch_to.window(handles[handle])
|
|
self.wait(0.2)
|
|
|
|
def switch_to_frame(self, element):
|
|
self.switch_to.frame(element)
|
|
|
|
def switch_to_parent_frame(self):
|
|
self.switch_to.parent_frame()
|
|
|
|
def switch_to_default(self):
|
|
self.switch_to.default_content()
|
|
|
|
def action_chains(self):
|
|
actions = ActionChains(self)
|
|
return actions
|
|
|
|
def minimize(self):
|
|
"""
|
|
浏览器窗口最小化;
|
|
"""
|
|
return self.minimize_window()
|
|
|
|
def maximize(self):
|
|
"""
|
|
浏览器窗口最大化;
|
|
"""
|
|
return self.maximize_window()
|
|
|
|
def fullscreen(self):
|
|
"""
|
|
浏览器进入全屏模式,注意:可能会因为新页面跳转导致全屏模式退出,并且浏览器窗口大小可能会被重设;
|
|
"""
|
|
return self.fullscreen_window()
|
|
|
|
|