我的Selenium代码登录到一个网站,然后到达一个带有iframe中存在的按钮的页面。检查chrome中的元素并使用浏览器的控制台,我发现iframe:
iframe = document.getElementById("deputy-app-view1045")
然后,我找到了元素并能够单击它:
iframe.contentWindow.document.getElementById("csv_download").click()
在我的python代码中,我插入了:
browser.execute_script('document.getElementById("deputy-app-view1043").contentWindow.document.getElementById("csv_download").click()')
但是,错误是"TypeError: document.getElementById(...) is null".
:(
[解决方案] 明白了!只需在js脚本中添加一个“return”!谢谢大家!旧版本:
browser.execute_script('document.getElementsByClassName("app-iframe dg-content-box margin-none Report Builder (BETA) ready")[0].contentWindow.document.getElementById("csv_download").href')
固定版本:
browser.execute_script('return document.getElementsByClassName("app-iframe dg-content-box margin-none Report Builder (BETA) ready")[0].contentWindow.document.getElementById("csv_download").href')
由于所需元素位于
<iframe>
内,因此要在元素上调用click()
,您必须:您可以使用以下任一Locator Strategies:
使用
CSS_SELECTOR
:使用
XPATH
:相关问题 更多 >
编程相关推荐