代码拉取完成,页面将自动刷新
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
import os
from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
class AutoClick():
def __init__(self, maximize_window=True, implicitly_wait=60,headless=False,restart=True):
if restart:
os.system('taskkill /f /im msedgedriver.exe')
os.system('taskkill /f /im msedge.exe')
os.system('cls')
executable_path = os.path.join(os.getcwd(),'msedgedriver')
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_experimental_option('excludeSwitches', ['enable-logging'])
# 设置无界面模式,也可以添加其它设置
if headless:
edge_options.add_argument('headless')
self.driver = Edge(executable_path=executable_path,options=edge_options)
self.imimplicitly_wait = implicitly_wait
self.init_window(maximize_window)
# 打开链接
def open(self, url):
if url:
self.driver.get(url)
# 界面截图
def screen_shot(self,name):
if name.find('.'):
name = name.split('.')[0]
self.driver.save_screenshot('{}.png'.format(name))
# 初始化浏览器
def init_window(self, *options):
maximize_window = options[0]
if maximize_window:
self.driver.maximize_window()
self.driver.set_page_load_timeout(self.imimplicitly_wait)
self.driver.set_script_timeout(self.imimplicitly_wait)
self.fast_wait = 3
# 退出并关闭窗口
def quit(self):
self.driver.quit()
# 关闭当前窗口
def close(self,count=1):
for i in range(count):
self.driver.close()
self.switch_window()
# 刷新F5
def refresh(self):
self.driver.refresh()
# 前进
def forword(self):
self.driver.forward()
self.switch_window()
# 后退
def back(self):
self.driver.back()
# 获取当前元素的文本
def text(self):
try:
self.execute()
return self.current_element.text
except Exception as e:
raise Exception()
# 获取当前Url
@property
def current_url(self):
return self.driver.current_url
# 等待
def sleep(self,second):
time.sleep(second)
# 窗口切换
def switch_window(self, window_index=0):
self.driver.switch_to.window(self.driver.window_handles[window_index-1])
def execute(self,script,*args):
return self.driver.execute_script(script,*args)
# 查找元素
def select(self,by, value,imimplicitly_wait=0,ignor_error=False):
if not imimplicitly_wait:
imimplicitly_wait = self.imimplicitly_wait
try:
self.current_element = WebDriverWait(self.driver,imimplicitly_wait,0.5).until(EC.visibility_of_element_located((by,value)))
except TimeoutException as e:
if ignor_error:
self.current_element=None
else:
raise TimeoutException()
return self
# 输入
def send_keys(self,text):
if self.current_element:
self.current_element.send_keys(text)
# 点击
def click(self,jump=True):
if self.current_element:
self.current_element.click()
if jump:
self.switch_window()
# 加载配置
def load_config(self,path=None):
self.config = dict()
if not path:
path = os.path.join("C:",'Users',os.getlogin(),'Desktop','config.txt')
with open(path) as file:
options = file.readlines()
for option in options:
option = option.strip().split('#')
key = option[0]
value = option[1]
self.config.update({key:value})
return self.config
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。