转载:https://blog.csdn.net/weixin_36279318/article/details/79475388
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
class BasePage:
def __init__(self):
global driver
self.driver = webdriver.Chrome()
driver = self.driver
driver.get('https://www.bilibili.com/video/BV1m4411w7dn?spm_id_from=333.999.0.0')
# 进入框架的关键字
def goto_frame(self, frame):
self.driver.switch_to.frame(frame)
# 出框架的关键字
def quit_frame(self):
self.driver.switch_to.default_content()
# 封装下拉框关键字
def choice_select(self, loc, value):
sel = Select(driver.find_element(By.XPATH, "//select[@id='nr']"))
Select(sel).select_by_value('50')
# 处理弹出窗口: alert(确定),confirm(确定、取消),prompt(确定、取消、输入值)
# accept点击确定,dismiss点击取消,text获得文本,send_keys输入值
def pop_window(self):
time.sleep(2)
aler = self.driver.switch_to.alert
aler.accept()
# 截取当前窗口,并指定截图图片的保存位置
def win_screenshot(self):
driver.get_screenshot_as_file("D:\\baidu_img.jpg")
# 鼠标悬停至“设置”链接
def action_test(self):
link = driver.find_element(By.PARTIAL_LINK_TEXT, '设置')
ActionChains(driver).move_to_element(link).perform()