基于Selenium的UI自动化实例-登录CSDN论坛
基于Selenium的UI自动化实例-登录CSDN论坛,处理iframe窗口,封装代码
·
# 导包
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
# 创建封装类
class Login:
# 创建属性参数
def __init__(self):
self.driver = webdriver.Chrome()
self.url = "https://www.csdn.net/"
self.driver.get("https://www.csdn.net/")
self.login_button_xpath = "//a[@class='toolbar-btn-loginfun']"
self.iframe_window_xpath = "//iframe[contains(@name,'passport_iframe')]"
self.login_to_password_xpath = "//*[text()='密码登录']"
self.username_input_xpath = "//input[@class='base-input-text'and @autocomplete='username']"
self.password_input_xpath = "//input[@class='base-input-text'and @autocomplete='current-password']"
self.agree_checkbox_xpath = "//i[@class='icon icon-nocheck']"
self.login_submit_button_xpath = "//button[@class='base-button']"
self.wait = WebDriverWait(self.driver, 10)
# 封装执行步骤
def login_action(self, login_num, login_password):
try:
self.driver.get(self.url)
self.driver.maximize_window()
self.driver.find_element(By.XPATH, self.login_button_xpath).click()
# 处理iframe窗口
self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, self.iframe_window_xpath)))
self.driver.find_element_by_xpath(self.login_to_password_xpath).click()
self.driver.find_element(By.XPATH, self.username_input_xpath).send_keys(login_num)
self.driver.find_element(By.XPATH, self.password_input_xpath).send_keys(login_password)
self.driver.find_element(By.XPATH, self.agree_checkbox_xpath).click()
self.driver.find_element(By.XPATH, self.login_submit_button_xpath).click()
sleep(3)
finally:
# 关闭浏览器
self.driver.quit()
# 实例化调用
login_instance = Login()
login_num = "your_num"
login_password = "your_password"
login_instance.login_action(login_num, login_password)
更多推荐




所有评论(0)