【问题标题】:Python Selenium. Can't click on a button蟒蛇硒。无法点击按钮
【发布时间】:2021-08-19 15:08:42
【问题描述】:

您好,我正在尝试单击此链接上的“下一步”按钮: https://www.sneakql.com/en-GB/launch/culturekings/womens-air-jordan-1-high-og-court-purple-au/register

我试过了,但是没用……

chrome.find_element_by_xpath('//*[@id="gatsby-focus-wrapper"]/main/div[2]/div[2]/div[2]/div/div/div[2]/div/form/div[3]/div/div/div/button').click();

我明白了:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button class="MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-fullWidth" tabindex="0" type="submit">...</button> is not clickable at point (456, 870). Other element would receive the click: <div class="jss3" style="flex: 1 0 300px; margin: 15px;">...</div>

【问题讨论】:

  • 你有什么错误吗?
  • 我明白了
  • selenium.common.exceptions.ElementClickInterceptedException:消息:元素单击被拦截:元素 在点 (456, 870) 不可点击。其他元素会收到点击:
    ...
  • 也显示您的代码

标签: python selenium selenium-webdriver xpath automated-tests


【解决方案1】:

“同意”按钮和“下一步”按钮具有相似的类名,因此“同意”按钮正在接收点击。

看看这是否有效:-


driver.find_element_by_xpath(".//button[contains(@class,'MuiButton-containedPrimary') and @type='submit']").click()

【讨论】:

  • 与以前相同的问题:selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素 在点 (456, 870) 不可点击。其他元素会收到点击:
    ...
    (Session info: chrome=90.0.4430.212)
  • 您必须通过此 xpath ".//button[@id='rcc-confirm-button']" 接受 cookie,然后尝试使用 xpath 作为 Next 按钮
  • 您非常接近完整答案。由于 cookie 按钮,点击无效。
【解决方案2】:

用JS直接点击就可以了

button = chrome.find_element_by_xpath('//*[@id="gatsby-focus-wrapper"]/main/div[2]/div[2]/div[2]/div/div/div[2]/div/form/div[3]/div/div/div/button')
chrome.execute_script("arguments[0].click();", button)

【讨论】:

  • 一旦 DOM 结构发生变化,即使这种方法也将停止工作。建议不要在 UI 自动化中使用完整的 xpath,因为它不稳定。但是当 Selenium 的点击即使使用短定位器也不起作用时,这是一个不错的选择。
【解决方案3】:

试试这个:

from selenium.webdriver.common.action_chains import ActionChains

next_btn = driver.find_element_by_xpath('//span[@class="MuiButton-label" and contains(text(),"Next")]')

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

如果这没有帮助 - 尝试添加一个简单的

time.sleep(5)

在点击该按钮之前

【讨论】:

  • 它没有找到按钮:selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:无法使用 xpath 表达式定位元素 //span[@class="MuiButton-label" 和contains(text()," 因为以下错误:SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[@class="MuiButton-label" and contains(text(),"'不是有效的 XPath 表达式。
  • 你是对的,对不起。我复制粘贴定位器的错误。我已经更正了答案
【解决方案4】:

您可以按照我在另一个问题中的建议单击它,使用xpath 并等待它可点击:

wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Next')]"))).click()

但在点击此按钮之前,您必须接受 cookie:

wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'AGREE')]"))).click()

为此,您需要导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2016-05-28
    • 2021-06-28
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2023-02-06
    相关资源
    最近更新 更多