首页 > 解决方案 > 单击贝宝“同意并继续”按钮时,.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')

我也试过 to_be_clickable

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

它就像一半的时间一样工作,这让我很困惑,有时当它不起作用时我会收到这个错误

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

(会话信息:chrome=91.0.4472.27)

标签: pythonselenium

解决方案


解决方案对我来说似乎相对容易。只需等待确切的文本:

button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, 
"//button[contains(text(), 'Agree &amp; Continue')]"
button.click()

或者,

//button[contains(text(), 'Agree & Continue')]

推荐阅读