繁体   English   中英

如何修复Python中的“元素不可交互” Selenium错误?

[英]How to fix “Element not interactable” Selenium error in Python?

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

“ selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互”。

我已经搜索了,但是找不到带有弹出按钮的解决方案。

单击显示10行并显示弹出框的图像随附的图像是所需的结果,并且在其后面有“显示10行”并可以轻松看到。

我的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中,我尝试了以下方法:

def single_meter(i=0):
browser=webdriver.Chrome('C:\Webdrivers\chromedriver.exe')
for row in range (5,10):
    browser.get('http://#link'+consumer_ID+'?reportrange=21%2F07%2F2018-25%2F08%2F2019')
        Show10 = find_element_by_xpath("//button[@class='dt-button buttons-collection buttons-page-length']//span[contains(text(),'Show 10 rows')]")
    Show10.click()

我希望单击此按钮,这将导致出现一个弹出按钮。

它可能在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)))

然后尝试单击按钮。

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

用以下命令更改xpath

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

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

导入此:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

尝试这个 :

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

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

暂无
暂无

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

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