from seleniumwire import webdriver
import unittest
from jiaoben.login.action.page_login import LoginPage
from selenium.webdriver.chrome.options import Options
class Login(unittest.TestCase):
def setUp(self):
options = Options()
options.add_experimental_option("detach", True) # 关键配置
self.driver = webdriver.Chrome(options=options)
self.driver.get("https://10.121.177.160")
def test_login(self):
print(f"开始登录")
LoginPage(self.driver).input_key(user='admin', passwd='admin')
# assert self.driver.title in "Chaoqiang G640 V7 10.121.176.137"
print("登录成功")
from selenium.webdriver.common.by import By
from jiaoben.Method_Library.base_action import BaseAction
class LoginPage(BaseAction):
user = (By.ID,"userid")
passwd = (By.ID,"password")
title_list = (By.XPATH, '//*[@id="btn-login"]')
def input_key(self, user, passwd):
self.input(self.user, user)
self.input(self.passwd, passwd)
self.find_element(self.title_list).click()
import time
from pyexpat import features
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.alert import Alert
class BaseAction:
def __init__(self, driver):
# 将传入的 driver 实例赋值给类的实例变量,供后续方法使用
self.driver = driver
cookies = self.driver.get_cookies()
#将cookies添加到浏览器当中,保持登录状态
for i in cookies:
self.driver.add_cookie(i)
#刷新页面,使添加的cookies生效
self.driver.refresh()
def find_element(self, feature, timeout=10, poll=1):
return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*feature))
def click(self, feature, timeout=10, poll=1):
self.find_element(feature, timeout, poll).click()
def input(self, feature, text, timeout=10, poll=1):
self.find_element(feature, timeout, poll).send_keys(text)
def clear_and_input(self, feature, text, timeout=10, poll=1):
self.clear(feature, timeout, poll)
self.find_element(feature, timeout, poll).send_keys(text)
def clear(self, feature, timeout=10, poll=1):
self.find_element(feature, timeout, poll).clear()
def get_text(self, feature, timeout=10, poll=1):
return self.find_element(feature, timeout, poll).text
def get(self, feature, timeout=10, poll=1):
return self.find_element(feature, timeout, poll)
def switch_new_page(self):
self.driver.switch_to.window(self.driver.window_handles[-1])
def switch_old_page(self):
self.driver.switch_to.window(self.driver.window_handles[0])
def find_elements(self, feature, timeout=10, poll=1):
return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(*feature))
def get_list(self, feature, timeout=10, poll=1):
return self.find_elements(feature, timeout, poll)
def move(self, feature, timeout=10, poll=1):
webElement = self.get(feature, timeout, poll)
self.driver.execute_script("arguments[0].scrollIntoView();",webElement)
def move_to(self, feature, timeout=10, poll=1):
webElement = self.find_element(feature, timeout, poll)
ActionChains(self.driver).move_to_element(webElement).perform()
def toset(self):
alert = Alert(self.driver)
alert.accept() # 点击确定/OK按钮
time.sleep(10)
# 通用属性获取
def get_value(self, feature, attribute_name, timeout=10, poll=1):
return self.find_element(feature, timeout, poll).get_attribute(attribute_name)
为什么会使得登录界面执行成功后一直停留在该页面
最新发布