#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)
'''
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
import time,os
import random
# about:addons 火狐浏览器安装组件,访问的地址
# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#id
keys="测试部落"
delay=3
#加载配置文件 使用 及富文本操作
# 配置文件地址:在浏览器中通过访问获得 帮助->故障排除信息->应用程序概要:配置文件夹
# C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\wv0f79j4.default
profile_path=r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\wv0f79j4.default'
profile=webdriver.FirefoxProfile(profile_path)
driver=webdriver.Firefox(profile)
# 新随笔按钮-发送富文本信息
# <a id="blog_nav_newpost" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a>
url="https://www.cnblogs.com/"
blogurl=url+"nicetime"
driver.get(blogurl)
#点击新随笔超连接
driver.find_element_by_id("blog_nav_newpost").click()
time.sleep(delay)
title=u'富文本标题01'
content=u'富文本内容01'
#帖子标题
driver.find_element_by_id("Editor_Edit_txbTitle").send_keys(title)
time.sleep(delay)
#点击上传文件按钮
driver.find_element_by_css_selector("img.mceIcon").click()
time.sleep(delay)
#转到文件上传的iframe 页面中有2个iframe,所以需要选择第2个iframe
image_iframe=driver.find_elements_by_tag_name("iframe")[1]
driver.switch_to.frame(image_iframe)
#上传文件 文件路径
file=u"D:\\jmeter\\jmeter3.2\\data\\cover.jpg"
driver.find_element_by_name("file").send_keys(file)
time.sleep(delay)
#方式1
#转到富文本iframe
#跳转到首页
driver.switch_to.default_content()
driver.switch_to.frame("Editor_Edit_EditorBody_ifr")
#帖子内容
driver.find_element_by_id("tinymce").send_keys(Keys.TAB)
driver.find_element_by_id("tinymce").send_keys(content)
time.sleep(delay)
#跳转到首页
driver.switch_to.default_content()
#方式2-通过js方式
driver.switch_to.default_content()
js='document.getElementById("Editor_Edit_EditorBody_ifr").contentWindow.document.body.innerHTML="%s"'%content
driver.execute_script(js)
#提交发布帖子
driver.find_element_by_id("Editor_Edit_lkbPost").click()
time.sleep(delay)
driver.quit()
转载于:https://www.cnblogs.com/NiceTime/p/10066814.html