Selenium2+python automation 23-rich text (automatic posting)

foreword

     The rich text editing box is the most common scenario for web automation. There are many small partners who do not know how to start. This article takes the editor of the blog garden as an example to solve how to locate rich text and enter text content.

1. Load configuration

    1. To open the blog garden to write an essay, you first need to log in. In order to avoid revealing personal account information, I directly load the configuration file and log in without it.

      If you don't know how to load configuration files, see this Selenium2+python automation 18-loading Firefox configuration

2. Open the editing interface

    1. Blog homepage address: bolgurl = "http://www.cnblogs.com/"

    2. My blog garden address: yoyobolg = bolgurl + "yoyoketang"

    3. Click the "New Post" button, id=blog_nav_newpost

Three, iframe switching

    1. After opening the editing interface, don't rush to enter the content, just sleep for a few seconds

    2. Enter the title, which can be located directly through the id, there is no difficulty

    3. The next step is to focus on editing rich text. There is an iframe in the edit box, so it needs to be switched first.

(If you don't understand iframe, you can read the previous article: Selenium2+python automation 14-iframe )

 

 Fourth, enter the text

    1. The positioning and editing text here is to locate the body part of the red box in the above figure, that is, id=tinymce

    2. After positioning, you can enter the content directly by the send_keys() method

    3. Some friends may not enter successfully, you can press a table key before entering, send_keys(Keys.TAB)

5. Reference code:

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

profileDir = r'C:\Users\Gloria\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default'
profile = webdriver.FirefoxProfile(profileDir)
driver = webdriver.Firefox(profile)

bolgurl = "http://www.cnblogs.com/"
yoyobolg = bolgurl + "yoyoketang"
driver.get(yoyobolg)
driver.find_element_by_id("blog_nav_newpost").click()

time.sleep(5)
edittile = u"Selenium2+python自动化23-富文本"
editbody = u"这里是发帖的正文"
driver.find_element_by_id("Editor_Edit_txbTitle").send_keys(edittile)
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(editbody)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325343219&siteId=291194637