首页 > 解决方案 > Python Selenium 查找所有 CSS 元素并替换所有

问题描述

我想在自动更改页面主题颜色的同时使用 Python Selenium 更改 CSS 元素样式。例如,页面元素如下:

<div style="background: #e7e7e7">
   <div style="border-bottom: #e7e7e7; border-top: #e7e7e7">

我想将颜色从 更改#e7e7e7#318c89,这意味着我需要e7e7e7在 HTML 脚本中找到所有内容并将所有内容替换为,#318c89但我不知道该怎么做。

提前致谢。

标签: pythonhtmlcssselenium

解决方案


尝试这个:

script = "document.body.innerHTML = document.body.innerHTML.replaceAll('#e7e7e7', '#318c89')"
driver.execute_script(script)

推荐阅读