首页 > 解决方案 > 如何通过 Python Selenium 更改网页源代码?

问题描述

我想制作将 pdf 文件嵌入到 chrome 中查看页面上的表格中的代码。理想情况下通过 Selenium Webdriver。这意味着我需要更改页面上的 html 代码

<td align="center">&nbsp;</td>

<embed src="http://qwer.com/a.pdf" type = "application/pdf">

现在我明白了你可以通过 driver.execute_script 启动 JavaScript,但是之前没有使用 JavaScript 的经验,我被卡住了。

据我所知,这是:

driver.execute_script("""document.evaluate('/html/body/div[9]/form/table/tbody/tr[10]/td[3]', document, null, XPathResult.ANY_TYPE, null).align="right";""")

该脚本尝试通过 XPath 选择 html 表格中的单元格并进行微小更改 - 对齐。它失败了。

我也一直在尝试更戏剧化的事情,但随着时间的推移,我什至无法弄清楚这个简单的事情,我恳求帮助。

感谢任何有建设性批评的人!

标签: javascriptpythonhtmlselenium-webdriver

解决方案


推荐阅读