我有一个 div,它有一个 even listener 与之链接。 div 通过单击展开并显示更多按钮。我试图通过单击打开 div,然后单击暴露的按钮。但是当我执行测试时,div 被点击,展开以显示其他按钮,然后自行折叠。但是,如果手动操作,它只会在单击两次后崩溃。因此,当我尝试单击暴露的按钮时,找不到它,我得到:以下错误可互换。
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
或
Element is not clickable at point . Other element would receive the click
我想附加的事件监听器可能通过加载太快/太慢而在其中发挥作用。看起来其他元素与目标元素重叠。如何单击目标按钮。
我试过在单击 div 和目标按钮之间给它一个 2 秒的休眠时间,但似乎只要单击 div 元素,它就会同时展开和折叠。我可以看到它这样做。
我也尝试使用下面的代码:
driver.execute_script("arguments[0].click();", element)
这是 html 代码片段:
<div class="software-package-top" id="package2_toggle">
<div class="software-package-info">
<div class="software-package-name">
name </div>
<div class="software-package-version">
3.0.1 </div>
<div class="software-package-status">
<span class="software-package-enabled">Disabled</span> <span class="software-package-not-running">Not Running</span> </div>
</div>
<div class="software-package-arrow"></div>
</div>
<div class="software-package-details software-hidden" id="package2_controls" style="display: none;">
<div class="software-package-description">
</div>
<hr>
<div class="software-package-controls">
<input type="button" name="View Release Notes" value="View Release Notes" id="package2_viewReleaseNotes" data-package="releaseNotes">
<input type="button" name="View Runtime Log" value="View Runtime Log" id="package2_viewLog">
<script>
$(document).ready(function() {
$("#package2_viewLog").click(function() {viewLog("---", "---", "no")});
});
</script>
<script>
$(document).ready(function() {
$("#package2_enable").click(function() {appControl("---", "---", "enable", "no")});
});
</script>
<input type="button" name="Enable" value="Enable" id="package2_enable">
<input type="button" name="Uninstall" value="Uninstall" id="package2_uninstall">
<script>
$(document).ready(function() {
$("#package2_uninstall").click(function() {appControl("---", "---", "uninstall", "no")});
});
</script>
</div>
</div>
</div>
最佳答案
所有这些错误信息...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
和
Element is not clickable at point . Other element would receive the click
...意味着您甚至在 JavaScript 之前调用了 click()
或 AJAX 调用完全呈现元素和完整的 DOM Tree .
解决方案
您需要为所需的 element_to_be_clickable()
引入 WebDriverWait,如下所示:
WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, element_xpath))).click()
关于python - 可扩展元素通过单击自行关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56499145/