selenium类

from inspect import trace
from xml.dom.minidom import Element
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import Chrome, ChromeOptions
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from logers import loggings
import traceback

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class chrome():
def init(self,vis:bool):
self.opt = ChromeOptions()
# self.opt.add_argument(‘–ignore-certificate-errors’)
if vis == True:
self.opt.headless = False # 把Chrome设置成可视化无界面模式,windows/Linux 皆可
else:
self.opt.headless = True
self.wd = Chrome(chrome_options=self.opt)
self.wd.maximize_window()
self.wd.implicitly_wait(1)

def open_website(self,url):
    self.wd.get(url)

def wait_click(self,xpath:str, maxtime:int):
    flag = 1
    time_count = 0
    while flag == 1 and time_count < maxtime:
        time_count += 1
        try:
            self.wd.find_element_by_xpath('{}'.format(xpath)).click()
            flag = 2
        except:
            time.sleep(1)
            pass

def wait_until(self,xpath:str, maxtime:int,log_flag:bool=False):
    wait_flag = 0
    time_count = 0
    while wait_flag == 0 and time_count < maxtime:
        time_count += 1
        try:
            element = self.wd.find_element_by_xpath('{}'.format(xpath))
            wait_flag = 1
        except:
            time.sleep(1)
            error =traceback.format_exc()
            pass
    if wait_flag == 0 & log_flag:
        loggings.error(error)
        return  
    elif wait_flag == 0:
        return
    return element

def wait_until_visable(self,xpath:str, maxtime:int,log_flag:bool=False):
    wait_flag = 0
    time_count = 0
    while wait_flag == 0 and time_count < maxtime:
        time_count += 1
        try:
            element = self.wd.find_element_by_xpath('{}'.format(xpath)).is_displayed()
            if element == True:
                wait_flag = 1
        except:
            time.sleep(1)
            error = traceback.format_exc()
            pass
    if wait_flag == 0 & log_flag:
        loggings.error(error)
        return  
    elif wait_flag == 0:
        return
    return element


def input(self, xpath:str , message , enter:bool=False, iswait:bool=True, max_wait_time:int=10):
    if iswait==True:
        self.wait_until(xpath,max_wait_time)
    if enter==False:
        self.wd.find_element_by_xpath(xpath).send_keys(message)
    else:
        self.wd.find_element_by_xpath(xpath).send_keys(message, Keys.ENTER)
        
# def input_file(self, xpath:str , message ,  iswait:bool=True, max_wait_time:int=10):
#     if iswait==True:
#         self.wait_until(xpath,max_wait_time)
        
#     self.wd.find_element_by_xpath(xpath).send_keys(message)


def clean_input(self, xpath:str, iswait:bool=True, max_wait_time:int=10):
    if iswait==True:
        self.wait_until(xpath,max_wait_time)
    
    self.wd.find_element_by_xpath(xpath).clear()

    
def click(self,xpath:str, iswait:bool=True, max_wait_time:int=10):
    if iswait==True:
        self.wait_until(xpath,max_wait_time)
    self.wd.find_element_by_xpath(xpath).click()
    
def move(self,xpath:str, iswait:bool=True, max_wait_time:int=10):
    if iswait==True:
        element = self.wait_until(xpath,max_wait_time)
    actions = ActionChains(self.wd)
    actions.move_to_element(element).perform()

def switch_frame(self,xpath:str, iswait:bool=True, max_wait_time:int=10):
    if iswait==True:
        self.wait_until(xpath,max_wait_time)
    frame = self.wd.find_element_by_xpath(xpath)
    self.wd.switch_to.frame(frame)

def move_bottom(self):
    # self.wd.execute_script("window.scrollTo(0,{})".format(self.wd.get_window_size()["height"]*2));
    self.wd.execute_script("window.scrollBy(0,0)");

def move_top(self,xpath:str):
    js ="document.getElementByXpath{}).scrollBy(0,800)".format(xpath)
    loggings.info(js)
    self.wd.execute_script(js);

def get_text(self,xpath:str, iswait:bool=True, max_wait_time:int=10):
    if iswait==True:
        self.wait_until(xpath,max_wait_time)
    text = self.wd.find_element_by_xpath(xpath).get_attribute("text")
    if text == None:
        text= self.wd.find_element_by_xpath(xpath).text
    if text == None:
        text= self.wd.find_element_by_xpath(xpath).get_attribute("textContent")
    if text == None:
        text= self.wd.find_element_by_xpath(xpath).get_attribute("innerText")
    return text 

#悬停
def move_on(self,el, max_wait_time:int=1):
    # if iswait==True:
    #     self.wait_until(xpath,max_wait_time) 
    # el = self.wd.find_element_by_xpath(xpath) 
    ActionChains(self.wd).move_to_element(el).perform()  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值