Python+Selenium input content to iframe rich text box

foreword

When using Selenium to test some CMS background systems, sometimes some rich text boxes are encountered, as shown in the following figure:

The entire rich text editor is embedded into the webpage through an iframe. Manually try to enter the content and find that the content is input into the body of the iframe page.

How to enter this kind of rich text box?
We can also directly right-click on the source code of the body and select Edit HTML, and enter the corresponding html code to achieve the purpose of inputting into the rich text box, as shown in the figure below:

Here's how to do it using Selenium

enter plain text only

If you only input plain text without formatting, you can switch to this iframe first, and then locate the corresponding text of body and send_keys. The code is 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')

After running, the display is as follows:

Inject HTML code via JS

If you want to input text with html format, you can inject it through js, the code is 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)
  • document in js script represents the entire document object
  • css selector syntax is used in querySelector() to locate the iframe frame
  • Use contentDocument to get the document object of the iframe
  • Use querySelector to locate the body node and modify its internal html code

After running, the effect is as follows:

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/m0_59868866/article/details/130363143