【问题标题】:Issues clicking a button (element not interactable) in Selenium using Python使用 Python 在 Selenium 中单击按钮(元素不可交互)的问题
【发布时间】:2021-12-17 18:48:21
【问题描述】:

我在单击 second 按钮时遇到问题,因为我收到了以下错误消息:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

html 按钮:

<button class="btn btn-primary btn-fab" type="button" 
    <span class="mdi mdi-plus" aria-hidden="true"></span>
    <span class="sr-only">+</span>

<button class="btn btn-primary btn-fab" type="button" 
    <span class="mdi mdi-minus" aria-hidden="true"></span>
    <span class="sr-only">-</span>
        

我的代码:

button = driver.find_element_by_css_selector('div.amount-control > button.btn.btn-primary.btn-fab > span.mdi.mdi-plus')
button.click()

我的代码所做的是定位 second 按钮,问题是我必须定位 &lt;span class="mdi mdi-plus" aria-hidden="true"&gt;&lt;/span&gt; 来区分这两个按钮,但我认为我必须点击 &lt;button class="btn btn-primary btn-fab" type="button" 而不是跨度,因为它不是可交互。

找到跨度后如何单击&lt;button&gt; 而不是跨度。

【问题讨论】:

  • 认为我在您的上一篇文章中为此提供了 xpath。

标签: python selenium


【解决方案1】:

您可以通过以下方式接收元素的父节点。

button = driver.find_element_by_css_selector('div.amount-control > button.btn.btn-primary.btn-fab > span.mdi.mdi-plus')
actual_button = button.find_element_by_xpath('./..')

此外,我建议您使用以下脚本单击 selenium 中的元素,因为 selenium 在单击元素时经常会出现问题。

try:
    element = driver.find_element(By.XPATH, path)
    driver.execute_script("arguments[0].click();", element)
except Exception as e:
    print(e)

请注意,您只需将 path 变量替换为您的 xpath。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2020-03-08
    • 2021-12-16
    • 2021-03-29
    • 2021-09-22
    相关资源
    最近更新 更多