首页 > 解决方案 > 如何使用 selenium Web 驱动程序 python 在 html 中的标签之间添加标签

问题描述

我想在网站的文本编辑器/文本框中插入像粗体这样的格式化文本,我该怎么做?

我试过这个:

browser.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div/div")
browser.execute_script("arguments[0].AdjacentHTML('beforebegin', arguments[1])", node, <'strong> bold </'strong>)

但是,这似乎在文本框内添加了与 HTML 相邻的内容。

标签: pythonhtmlpython-3.xselenium

解决方案


我会使用 js 方法'setAttribute':

browser.execute_script("arguments[0].setAttribute('style', arguments[1]);",node, "font-weight: bold");

推荐阅读