首页 > 解决方案 > 如何在使用 selenium(python3)打开的网站中修改 html 代码?

问题描述

我有一个 python 脚本,可以打开一个带有 selenium 的网站。打开后,我希望 selenium 更改网站中某些按钮的 html 中的某些行。

这是我使用 chrome 开发工具检查页面时的实际代码

<button type="button" class="achieve-btn">Sometext &amp; Sometext</button>

这就是我想用它修改代码

<button type="button" class="getExamButtonId1" onclick="openInstructionsPage(6012,'getExamButtonId1','incompleteExamId10001861')">Sometext &amp; Sometext</button>

有可能吗?

如果可能,请提供要在我的脚本中编写的确切代码行以执行以下操作。

标签: python-3.xseleniumselenium-chromedriver

解决方案


您需要尝试修改innerHTML此元素的属性。

element = driver.find_element_by_css_selector("button.achieve-btn")

driver.execute_script("arguments[0].innerHTML = 'your_html_code_here'", element)

推荐阅读