如何修复Python中的“Element not interactiveable”Selenium错误?

2024-06-26 14:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图单击一个弹出按钮,代码显示错误:

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

我已经搜索,但找不到解决方案,我与弹出按钮。在

Image for clicking show 10 rows and displaying the pop-up box所附图像是所需的结果,“Show 10 rows”位于其后面,并且很容易看到。在

我有这个在我的HTML代码,需要点击按钮。在

<div class="table-responsive">
<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" id="loadsur" href="#Section" aria-expanded="true">LoadSurvey</a></li>
</ul>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12" style="margin-left: -10px;">
            <div class="table-responsive">
                <div id="myDataTable25_wrapper" class="dataTables_wrapper no-footer"><div class="dt-buttons">
                <button class="dt-button buttons-csv buttons-html5" tabindex="0" aria-controls="myDataTable25">
                   <span>Csv</span></button> 
                <button class="dt-button buttons-excel buttons-html5" tabindex="0" aria-controls="myDataTable25">
                   <span>Excel</span></button> 
                <button class="dt-button buttons-collection buttons-page-length" tabindex="0" aria-controls="myDataTable25" aria-haspopup="true">
                   <span>Show 10 rows</span></button> 
                </div>
            </div>
        </div>
    </div>
</div>

在Python中,我尝试了以下方法:

^{pr2}$

我希望单击这个按钮,这会导致弹出按钮出现。在


Tags: 代码divshowdttablebutton按钮class
3条回答

它可能在iframe中。在

首先尝试找到iframe并切换到它。在

browser = webdriver.Chrome()
browser.get("http:/link") 
frame_id = 'frame'
wait = WebDriverWait(browser, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, frame_id)))

然后尝试单击按钮。在

^{pr2}$

xpath更改为:

//button[@class='dt-button buttons-collection buttons-page-length']//span[contains(text(),'Show 10 rows')]

尝试使用WebDriverWait确保元素存在,并添加expected_conditions直到元素clickable。在

导入此:

^{pr2}$

试试这个:

wait = WebDriverWait(singlemeter, 10)
Show10 = wait.until(expected_conditions.element_to_be_clickable((By.XPATH, "//button[@class='dt-button buttons-collection buttons-page-length']//span[contains(text(),'Show 10 rows')]")))
Show10.click()

或使用ActionChains,导入:

from selenium.webdriver import ActionChains

试试这个:

ActionChains(singlemeter).move_to_element(Show10).click(Show10).perform()

我找到了解决这个问题的办法。xpath中的链接出错。我后来从html中复制并粘贴,现在代码如下:

Show10 = find_element_by_xpath("//*[@id='myDataTable2_wrapper']/div[1]/button[7]/span")
Show10.click()

而且效果很好。谢谢大家的帮助。在

相关问题 更多 >