【问题标题】:How to fix "Element not interactable" Selenium error in Python?如何修复 Python 中的“元素不可交互”Selenium 错误?
【发布时间】:2019-08-26 02:08:28
【问题描述】:

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

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

我已经搜索但找不到适合我的弹出按钮解决方案。

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 中我试过这个:

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

我希望单击此按钮会出现一个弹出按钮。

【问题讨论】:

  • span 不是button,即使它看起来像按钮并且无法单击。你必须找到button并点击它。
  • 我也尝试使用按钮,但出现相同的错误消息。请有任何进一步的建议。
  • HTML 中有iframe 吗?
  • @MosheSlavin 最后有 iframe 只有这么多
  • @MosheSlavin 所附图像是所需的结果,“显示 10 行”在其后面并且可以轻松看到。

标签: python html python-3.x selenium


【解决方案1】:

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

【讨论】:

  • @Moshe Slavin 一件事必须是单表,它是否正确:singlemeter=browser.get("http:/link) 和 browser=webdriver.chrome() ??
  • @AbanishTiwari 我看不出有理由将get 放入变量中。 get() 导航到一个 url,但它不是 webdriver
  • @AbanishTiwari 你可以试试 Moshe Slavin 的建议,它可能在 iframe 中,或者请发布更多代码,我们会尽力解决你的问题。
  • @Moshe Slavin 谢谢,我后来意识到没有必要将 .get () 分配给变量。我按照上面提到的更改运行,但它引发了一个错误: :::::::::::::: raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message : :: ::::::找不到元素的原因可能是什么?
  • @farianH 我试过了。好的,我会向您发送更多代码。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 我编辑改成了这个:::::::wait = WebDriverWait(singlemeter, 10) Show10 = wait.until(expected_conditions.element_to_be_clickable((By.XPATH,"//button[@ class='dt-buttonbuttons-collectionbuttons-page-length']//span[contains(text(),'Show 10 rows')]"))) Show10.click() :::::::: :::: 但它找不到引发错误“selenium.common.exceptions.TimeoutException: Message:”
    • selenium.common.exceptions.TimeoutException: Message: 这是错误,因为在指定时间段内未找到元素
    • 这是使用actionchain的正确方法吗? :::::: wait = WebDriverWait(singlemeter, 10) Show10 = wait.until(expected_conditions.element_to_be_clickable((By.XPATH,"//button[@class='dt-buttonbuttons-collectionbuttons-page-length' ]//span[contains(text(),'Show 10 rows')]"))) ActionChains(load_survey).move_to_element(Show10).click(Show10).perform()::::::::: I我是 actionChains 的新手。这对我不起作用。
    • @AbanishTiwari ActionChains 除了驱动程序...在您的情况下,您将其称为singlemeter,因此请使用:ActionChains(singlemeter).move_to_element(Show10).click(Show10).perform(),如答案所示!
    • 好答案!结合 WebDriverWaitActionChains 应该可以解决问题!
    猜你喜欢
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多