首页 > 解决方案 > 使用硒将数据插入标签

问题描述

我有一个页面,我无法单击带有 selenium 的按钮,在单击按钮之前,html 看起来像这样: 在此处输入图像描述

这就是按下按钮后的样子: 在此处输入图像描述

有一种方法可以将该值插入到 html 标记中,以便在案例按钮中获得我想要的元素的可见性。我尝试使用 WebDriverWait 和 ActionChains 但没有成功。

我执行javascript代码:

driver.execute_script('document.getElementsByTagName("html")[0].setAttribute("data-whatelement", "button")')
driver.execute_script('document.getElementsByTagName("html")[0].setAttribute("data-whatclasses", "Button__StyledButton-iESSlv,dJJJCD Button-dtUzzq kHUYTy")')

而且按钮元素仍然不可见。我怎样才能让元素尝试使用 EC 等待和操作链。它是否超时异常。任何建议。

标签: pythonhtmlseleniumdomautomation

解决方案


使用execute_script您可以运行 javascript 并随意操作 DOM。请参阅:使用 Python 在 Selenium 中运行 javascript

因此,例如(我不完全确定您需要做什么才能获得元素的可见性),将data-inq-observer属性重新插入body标签:

driver.execute_script('document.getElementByTagName("body").setAttribute("data-inq-observer", "1")')

推荐阅读