繁体   English   中英

使用 Python 在 Selenium 中单击按钮(元素不可交互)时出现问题

[英]Issues clicking a button (element not interactable) in Selenium using Python

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

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()

我的代码所做的是找到第二个按钮,问题是我必须找到<span class="mdi mdi-plus" aria-hidden="true"></span>来区分两个按钮,但我想我必须点击<button class="btn btn-primary btn-fab" type="button"不是跨度,因为它不可交互。

找到跨度后如何单击<button>而不是跨度。

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

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。

您的元素未正确关闭。 在这两种情况下都缺少</button>结束标记。 请将您的代码更改为:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM