简体   繁体   English

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

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

I was trying to click a popup button and my code shows the error: 我试图单击一个弹出按钮,我的代码显示错误:

"selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable". “ selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互”。

I have searched but could not find the solution which works for me with the popup buttons. 我已经搜索了,但是找不到带有弹出按钮的解决方案。

Image for clicking show 10 rows and displaying the pop-up box The attached image is the desired result and 'Show 10 rows' is behind it and lightly seen. 单击显示10行并显示弹出框的图像随附的图像是所需的结果,并且在其后面有“显示10行”并可以轻松看到。

I have this in my HTML code and need to click on the button. 我的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>

In Python I tried this: 在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()

I expect to click this button which causes a popup button to appear. 我希望单击此按钮,这将导致出现一个弹出按钮。

It might be in an iframe . 它可能在iframe

Try first to find the iframe and switch to it. 首先尝试找到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)))

Then try clicking on the button. 然后尝试单击按钮。

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

Change the xpath with : 用以下命令更改xpath

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

Try use WebDriverWait for make sure the element exist, and add expected_conditions until the element clickable . 尝试使用WebDriverWait确保该元素存在,并添加expected_conditions直到元素clickable为止。

Import this : 导入此:

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

Try this : 尝试这个 :

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

Or use ActionChains , import this : 或使用ActionChains ,导入以下内容:

from selenium.webdriver import ActionChains

Try this : 尝试这个 :

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

I got a solution to this problem. 我已经解决了这个问题。 The error was with the link in the xpath. 错误是与xpath中的链接有关。 I later copied and pasted from the html and the code now looks this: 我后来从html复制并粘贴,代码现在看起来像这样:

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

And it works fine. 而且效果很好。 Thank all for your help. 谢谢大家的帮助。

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

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