首页 > 解决方案 > 在这种情况下,如何在 selenium 上选择更改的按钮

问题描述

我正在尝试等待“同意并继续”按钮出现并与 .click() 交互

该按钮基本上以“继续按钮”开始,然后变为“同意并继续”。

在“继续”按钮之前:

<button class="ppvx_btn___5-8-2" aria-live="assertive" id="payment-submit-btn" data-testid="submit-button-initial" data-disabled="true" xpath="1">Continue<span class="ppvx_btn--state__screenreader___5-8-2"></span></button>

在“同意并继续”按钮后:

<button class="ppvx_btn___5-8-2" aria-live="assertive" id="payment-submit-btn" data-testid="submit-button-initial" data-disabled="false" xpath="1">Agree &amp; Continue<span class="ppvx_btn--state__screenreader___5-8-2"></span></button>

我试过的:

mainagree = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//button[@id='payment-submit-btn'][@data- 
disabled='false']")).click()
print('clicked')

它有时会起作用,但并不一致。

有时,我认为它单击了上一个“继续”按钮,当它不起作用时,我收到以下错误消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (951, 965)

(会话信息:chrome=91.0.4472.27)

标签: pythonseleniumclick

解决方案


element click intercepted: Element is not clickable at point问题可能是由几个问题引起的,因此我们无法知道您的情况到底发生了什么。
可能您应该使用invisibility_of_element_located让其他一些元素消失,或者使用 JavaScript click 或只是添加一些延迟。
有关这一点的良好解释,请参见此处

如果您的元素不在屏幕上,您应该滚动到它
这里是如何滚动的示例

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("my-id")

actions = ActionChains(driver)
actions.move_to_element(element).perform()

推荐阅读