Selenium content to enter rich text box iframe

Foreword

When using Selenium to test some CMS back-office systems, sometimes encounter some rich text box, as shown below:

The entire rich text editor is embedded in, try to manually input the contents of a Web page via iframe, found that the content is input to the body iframe page's,

How this rich text input box it?
We can also right-click directly on the body of the source select Edit HTML, enter the appropriate html code to achieve the purpose of the rich text input box, as the following figure:

Here is the method of operation of Selenium

Enter only plain text

If only the plain text with format can be switched to the first iframe, and then locate the body, corresponding to send_keys text, as follows:

from selenium import webdriver
dr = webdriver.Chrome()

dr.get('http://www.vemmis.com/bjq/index.html')

dr.switch_to.frame('ueditor_0')
dr.find_element('tag name', 'body').send_keys('hello')

Run to completion, it is shown below:

By injecting HTML code JS

To enter text with js html format by injection, as follows:

from selenium import webdriver
dr = webdriver.Chrome()

dr.get('http://www.vemmis.com/bjq/index.html')

js = "document.querySelector('#ueditor_0').contentDocument.querySelector('body').innerHTML='<h1>Hello</h1>'"
dr.execute_script(js)
  • js script document on behalf of the entire document object
  • querySelector css selector syntax used to target the iframe frame () in
  • ContentDocument get to use iframe document object
  • Use querySelector body targeting node, to modify its internal html code

Run finished effect is as follows

Guess you like

Origin www.cnblogs.com/superhin/p/12051423.html