如何给网页中的测试添加背景色?

How to add background color to the test in webpage?

我使用以下代码在网页中找到了一些文本

document.body.innerText.indexOf('tested')
document.body.innerText.includes('tested')

找到文本后,我想为该文本应用背景色。

使用您的 innerText 查找元素的引用:

let xpath = "//div[text()='tested']";
let element = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

div 替换为出现文本的任何类型的元素

有了元素引用就可以直接修改样式了:

element.style.backgroundColor = "red";