【问题标题】:the OK button can't be click - why?确定按钮不能点击 - 为什么?
【发布时间】:2017-11-27 08:36:28
【问题描述】:

下面是弹出窗口内“确定”按钮的 ID,只是想说我能够在同一个弹出窗口内为另一个字段发送键和 ENTER: OK button

我得到的例外是:

selenium.common.exceptions.ElementNotInteractableException: Message: Cannot click on element

还添加了实际的 GUI: actual window

代码如下:

def create_new_customer_iden():
    browser.find_element_by_css_selector("#customer_care_app_tab > tbody > tr > td:nth-child(2)").click()
    browser.find_element_by_css_selector("#add_customer > td:nth-child(2)").click()
    browser.find_element_by_id("go_srch_button").click()
    random_number=random.randrange (10000, 99999)
    statement ='INSERT INTO sa_customer(CUSTOMER_NO, CUSTOMER_NAME) VALUES(:1, :2)'
    cursor.execute(statement,(random_number,random_number))
    connection.commit()
    browser.find_element_by_name("popup_search_init_customer_id").send_keys(random_number)
    browser.find_element_by_name("popup_search_init_customer_id").send_keys(Keys.ENTER)
    # works great until here
    browser.find_element_by_id("ok_button").click()

我发现了另一个问题: 如果我添加以下代码行:

button=browser.find_element_by_id ("ok_button")
print(button)
button.click()

印刷品给了我这个元素:

selenium.webdriver.remote.webelement.WebElement (session="75fba3d3-bef4-4800-a03d-bfbfcf831209", element="4d27c723-ed64-40a5-9da0-102a52f4d910")

但最后一行代码向我抛出了以下异常:

selenium.common.exceptions.ElementNotInteractableException: Message: Cannot click on element

这意味着我需要找到 click() 操作的替代品。 有什么建议吗???

【问题讨论】:

标签: python python-3.x selenium-webdriver webdriver


【解决方案1】:

在弹出窗口加载后尝试以下操作之一 - driver.switch_to_frame(webelement) / driver.switch_to_window(window_name)

一旦您完成与弹出窗口的交互,例如你已经关闭它使用driver.switch_to_default_content()返回主页

window_name 可以由

定义

var window_name = driver.find_element_by_class_name("popup_table")

如果以上方法都不起作用,我建议尝试的最后一件事是:driver.switch_to_alert()

更多信息请见:Python webdriver to handle pop up browser windows which is not an alert

Switch to popup in python using selenium

我还建议阅读 Python 文档:http://selenium-python.readthedocs.io/navigating.html - 如果以上内容没有帮助,您可能会在这里找到答案。我对 Python 不是很熟悉

【讨论】:

【解决方案2】:

试试这个,然后看看抛出了什么异常?

wait = WebDriverWait(driver, 10)
element = wait.until(expected_conditions.element_to_be_clickable((By.ID, "ok_button")))

【讨论】:

  • 异常:引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
【解决方案3】:

使用XPathselector 怎么样? Chrome DevTools 提供了它们。

这是示例代码

browser.find_element_by_xpath("insert ok_button xpath").click()
browser.find_element_by_css_selector("insert ok_button selector").click()

【讨论】:

    【解决方案4】:

    “打印给了我这个元素:selenium.webdriver.remote.webelement.WebElement (session="75fba3d3-bef4-4800-a03d-bfbfcf831209", element="4d27c723-ed64-40a5-9da0-102a52f4d910") 但是最后一行代码向我抛出了以下异常:selenium.common.exceptions.ElementNotInteractableException:消息:无法单击元素这意味着我需要找到 click() 操作的替代品。有什么建议吗???”

    是的,试试下面的:

    def create_new_customer_iden():
        browser.find_element_by_css_selector("#customer_care_app_tab > tbody > tr > td:nth-child(2)").click()
        browser.find_element_by_css_selector("#add_customer > td:nth-child(2)").click()
        browser.find_element_by_id("go_srch_button").click()
        random_number=random.randrange (10000, 99999)
        statement ='INSERT INTO sa_customer(CUSTOMER_NO, CUSTOMER_NAME) VALUES(:1, :2)'
        cursor.execute(statement,(random_number,random_number))
        connection.commit()
        browser.find_element_by_name("popup_search_init_customer_id").send_keys(random_number)
        browser.find_element_by_name("popup_search_init_customer_id").send_keys(Keys.ENTER)
    
    element = browser.find_element_by_class_name("popup_table")
    browser.switch_to_window(window_name)
    
    okButton = browser.find_element_by_id("ok_button")
    actions = ActionChains(browser)
    actions.move_to_element(okButton).perform()
    actions.click(okButton)
    //some action to close the popup
    browser.switch_to_default_content()
    

    如果没有,试试这个...

    def create_new_customer_iden():
        browser.find_element_by_css_selector("#customer_care_app_tab > tbody > tr > td:nth-child(2)").click()
        browser.find_element_by_css_selector("#add_customer > td:nth-child(2)").click()
        browser.find_element_by_id("go_srch_button").click()
        random_number=random.randrange (10000, 99999)
        statement ='INSERT INTO sa_customer(CUSTOMER_NO, CUSTOMER_NAME) VALUES(:1, :2)'
        cursor.execute(statement,(random_number,random_number))
        connection.commit()
        browser.find_element_by_name("popup_search_init_customer_id").send_keys(random_number)
        browser.find_element_by_name("popup_search_init_customer_id").send_keys(Keys.ENTER)
    
    driver.switch_to_alert()
    
    okButton = browser.find_element_by_id("ok_button")
    actions = ActionChains(browser)
    actions.move_to_element(okButton).perform()
    actions.click(okButton)
    //some action to close the popup
    browser.switch_to_default_content()
    

    显然,您可能需要稍作更改以适应您的代码,例如浏览器而不是驱动程序。

    https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html

    【讨论】:

    • 什么都没有发生 - 它只是卡在最后一行代码 - 没有点击 OK 按钮并且弹出窗口的屏幕永远保持打开状态
    • 仍然没有任何反应 - 只是卡在弹出窗口上而不是点击g
    • 先试试这个 driver.switch_to_alert()
    • 不,还添加了那个并且仍然冻结
    • 嗯,我不太确定,如果您提供该页面的网络链接,那么我可能会看看
    【解决方案5】:

    好的,我最终解决了这个问题: 似乎有 2 个按钮具有相同的 ID - 所以我添加了如下索引:

    browser.find_element_by_xpath('(//*[@id="ok_button"])[2]').click()
    

    成功了!!!!

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 2020-06-12
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      相关资源
      最近更新 更多