您的位置:首页 > 编程语言 > Python开发

Selenium Python自动化测试学习知识积累-操作iframe富文本框

2017-12-26 16:19 351 查看
1、打开htpp://mail.sohu.com

2、输入用户名

3、输入密码

4、点击登录按钮

5、点击“写邮件”按钮

6、输入收件人

7、输入邮件标题

8、输入邮件正文内容

9、点击发送按钮

# encoding=utf-8
from selenium import webdriver
import unittest
import time
import traceback
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

class TestDemo(unittest.TestCase):

def setUp(self):
# 创建存储自定义文件配置文件的路劲变量
propath = "C:\\Users\\ifthink\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\xf1mvsab.default"

# 加载自定义配置文件到firefoxprofile实例中
profile = webdriver.FirefoxProfile(propath)

# 启动带自定义配置文件的浏览器
self.driver = webdriver.Firefox(executable_path="F:\\Python\\Webdriver\\geckodriver.exe",
firefox_profile = profile)

def test_sohumailsendmail(self):
url = "http://mail.sohu.com"

# 访问首页
self.driver.get(url)

try:
wait = WebDriverWait(self.driver, 10)
wait.until(ec.element_to_be_clickable((By.XPATH, "//input[@placeholder='请输入您的邮箱']")))

username = self.driver.find_element_by_xpath("//input[@placeholder='请输入您的邮箱']")
username.clear()
username.send_keys("xxxx@sohu.com")

password = self.driver.find_element_by_xpath("//input[@placeholder='请输入您的密码']")
password.clear()
password.send_keys("xxxxxx")

login = self.driver.find_element_by_xpath("//input[@value='登 录']")
login.click()

wait.until(ec.element_to_be_clickable((By.XPATH, "//li[text()='写邮件']")))
self.driver.find_element_by_xpath("//li[text()='写邮件']").click()

time.sleep(2)

receiver = self.driver.find_element_by_xpath("//div[@arr='mail.to_render']//input")
# 输入收件人
receiver.send_keys("ifthink@sohu.com")

subject = self.driver.find_element_by_xpath("//input[@ng-model='mail.subject']")
# 输入邮件标题
subject.send_keys("这是一封测试邮件")

# 获取邮件正文编辑区域的iframe页面元素对象
iframe = self.driver.find_element_by_xpath("//iframe[contains(@id,'ueditor_0')]")
# 通过switch_to.frame()方法切换进入富文本框
self.driver.switch_to.frame(iframe)

# 获取富文本框中编辑页面元素对象
editbox = self.driver.find_element_by_xpath("//html/body")

editbox.send_keys("邮件的正文内容")

# 跳出富文本框
self.driver.switch_to.default_content()

# 找到发送按钮,并点击它
self.driver.find_element_by_xpath("//span[text()='发送']").click()

# 显示等待含有关键字串“发送成功”的页面元素出现在页面中
wait.until(ec.visibility_of_element_located((By.XPATH, "//span[.='发送成功']")))
print("邮件发送成功")

except TimeoutException:
print("显示等待页面元素超时")
except NoSuchElementException:
print("寻找的页面元素不存在")
except Exception:
print(traceback.print_exc())

if __name__ == '__main__':
unittest.main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息