selenium+Python富文本框賦值


################################

富文本框賦值
富文本框是指<iframe></iframe>
首先訪問iframe
再訪問iframe中的contentWindow
再訪問body  
使用innerText屬性進行賦值(innterHtml)
self.set_content(
'content content') def set_content(self, text): js = "document.getElementById('content_ifr').contentWindow.document.body.innerText = '%s'" %(text) self.driver.execute_script(js)



iframe1 = document.getElementsByTagName("iframe")[1]

innertext = iframe1.contentWindow.document.body.innerText

js = "document.getElementsByTagName("iframe")[1].contentWindow.document.body.innerText = '%s'" %(text)
 
         
text = "我要提問一個問題"
js = "document.getElementsByTagName('iframe')[1].contentWindow.document.body.innerText = '%s'" % (text)
driver.execute_script(js)
 

 

####################################

定位iframe

# 1.有id,並且唯一,直接寫id
driver.switch_to_frame("x-URS-iframe")
driver.switch_to.frame("x-URS-iframe")

# 2.有name,並且唯一,直接寫name
driver.switch_to_frame("xxxx")
driver.switch_to.frame("xxxx")

# 3.無id,無name,先定位iframe元素
iframe = driver.find_elements_by_tag_name("iframe")[0]
driver.switch_to_frame(iframe)
driver.switch_to.frame(iframe)

# 4.通過index索引定位,(從0開始)
driver.switch_to_frame(1) # 第二個

# 使用iframe定位后需退出iframe
# 退出iframe,再操作
driver.switch_to_default_content() # 回到主頁面
driver.switch_to.default_content()

 

######################################

 

 

 

######################################


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM