selenium's handling of rich text

This article is reproduced in the current study

Due to project needs, rich text processing is involved. Baidu, found that someone has summed up a lot.

The general idea is as follows:

1.switch_to_frame

2.find_element_by_tag_name('body').send_keys("123") 

Link: http://blog.csdn.net/huilan_same/article/details/52386274

There are three types of input boxes:

  1. The short input box is as follows:

    <input id="zenInput2" class="zenInputDemo" type="text" style="position: static;">
    
  2. textarea box, as follows:

    <textarea id="message1" name="message1"></textarea>
    
  3. The div editor box is as follows:

    See the code Web page source code

  4. It may also be a more complex editor for iframe, as follows:

    See the code Web page source code

Let's take a look at how these input boxes can be solved in turn.

1. input

Actually, this is just listed here. How to deal with input? I want to know what selenium knows how to do.

2.textarea

Simply locate the element and send_keys directly.

Example Web site: http://www.sucaijiayuan.com/api/demo.php?url=/demo/20150325-1

Code:

# -*- coding: utf-8 -*-

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get('http://www.sucaijiayuan.com/api/demo.php?url=/demo/20150325-1')
driver.maximize_window()

driver.switch_to.frame('iframe')

driver.find_element_by_id('message1').send_keys('Hello world!')  # Simply send_keys
sleep(2)

print driver.find_element_by_id('message1').get_attribute('value')

driver.quit()

 

 

Result:

Hello world!

 

3.div editor

Similarly, locate the element div and send_keys directly, but this send_keys is not in the'value'attribute, but in the text.

Example Web site: http://www.sucaijiayuan.com/api/demo.php?url=/demo/bootstrap-based lightweight jQuery text editor plug-in%20LineControl/index.html

# -*- coding: utf-8 -*-

from selenium import webdriver
from time import sleep


driver = webdriver.Firefox()
driver.get('http://www.sucaijiayuan.com/api/demo.php?url=/demo/%E5%9F%BA%E4%BA%8Ebootstrap%E7%9A%84%E8%BD%BB%E9%87%8F%E7%BA%A7jQuery%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8%E6%8F%92%E4%BB%B6%20LineControl/index.html')
driver.maximize_window()

driver.switch_to.frame('iframe')

driver.find_element_by_class_name('Editor-editor').send_keys('Hello world again!')  # It doesn't make any difference. It's also direct send_keys.
sleep(2)

print driver.find_element_by_class_name('Editor-editor').text

driver.quit()

 

Result:

Hello world again!

4. editor in iframe

This is the most complex one, but to understand, in fact, it is also very simple.

Example Web site: http://ueditor.baidu.com/website/examples/completeDemo.html

Code:

# -*- coding: utf-8 -*-

from selenium import webdriver

driver = webdriver.Chrome(executable_path='D:\py\AutoTestFramework\drivers\chromedriver.exe')
driver.get('http://ueditor.baidu.com/website/examples/completeDemo.html')

driver.switch_to.frame('ueditor_0')  # Note that this editor must have a frame and must cut the frame.

body_string = """Hello world again again!
Hello world again again!
Hello world again again!

Hello world again again!"""

driver.find_element_by_tag_name('body').send_keys(body_string)  # Is it simple and rude to just fill in the body in the frame?
print driver.find_element_by_tag_name('body').text
driver.quit()

Result:

Hello world again again!

In fact, the content of frame editor is usually written in the body inside. The most important thing is to cut into the frame. For the location of frame in switch, see my blog:


Location of selenium and switching frame (iframe)

The frame is usually an empty html, where the content displayed is the content in the body.

Keywords: Selenium PHP Firefox Attribute

Added by el_quijote on Sat, 08 Jun 2019 23:29:13 +0300