当单击按钮时发生的Selenium错误:.MoveTargetOutOfBoundsException

2024-03-28 08:54:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python上使用safari和selenium,我想制作一个python selenium程序,在iframe中单击'confirm'按钮,但它给了我一个错误或者不起作用(按钮和iframe在viewport中)

基本上不管怎样,我都会尝试,如果用“action chains”点击按钮,它会给我同样的错误(selenium.common.exceptions.MoveTargetOutOfBoundsException: Message:),或者如果正常点击按钮,它不会有任何结果(不起作用)

Html-iframe

<iframe title="Confirm-box" scrolling="no" class="bottom" data-name="bottom">
</iframe>

Html-按钮

<button type="submit" class="btn btn-submit" name="submitBtn">
    Confirm
</button>

iframe

iframe2 = driver.find_elements_by_tag_name('iframe')[1]

滚动到按钮

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
driver.switch_to.frame(iframe2)

双击按钮

actionChains = ActionChains(driver)
element = driver.find_element_by_xpath("//button[contains(text(),'Confirm')]")
actionChains.double_click(element).perform()

预期产量:

按钮已单击

错误(操作链):

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message:

Tags: namemessagehtmldriverselenium错误buttonelement
1条回答
网友
1楼 · 发布于 2024-03-28 08:54:34

您不能直接单击iframe元素,首先,您需要使用seleniumswitchTo中的一个特殊命令,该命令允许您更改可以在iframe、windows和alerts之间切换的上下文。你知道吗

我不知道python的语法,但应该是这样的:

driver.switchTo().iframe('iframe');

你可以在这里看到官方文件: https://www.seleniumhq.org/docs/03_webdriver.jsp#moving-between-windows-and-frames

相关问题 更多 >