【问题标题】:How can I interact with this modal dialog using Webdriver & Python?如何使用 Webdriver 和 Python 与此模式对话框进行交互?
【发布时间】:2013-04-11 14:54:00
【问题描述】:

我想要的只是关闭一个模态对话框,最好是执行以下操作:

browser.find_element_by_link_text("OK").click()

为 OK 链接文本提供 NoSuchElementException: Message: u'The element could not be found'

当我这样做时,xpath 也是如此:

browser.find_element_by_xpath("//*[@id=\"modal\"]/div/div[2]/div/a").click()

我怀疑这是因为我需要将注意力放在对话上。为此,我尝试过:

for handle in browser.window_handles:
    browser.switch_to_window(handle)
    if browser.find_element_by_class_name('popUp123')
        browser.find_element_by_link_text("OK").click()

为班级提供NoSuchElementException: Message: u'The element could not be found'

也试过browser.switch_to_frame(ID OR NAME),但也找不到框架。

请告诉我,我遗漏了一些明显的东西。

相关框架来源(总结):

<body id="modal">
    <div class="popUp123">
    <div class="button">
        <div class="centerbutton">
            <a href="#" class="close" onclick=parent.close">
                <span>OK</span>

【问题讨论】:

  • 如果你在代码前加上time.sleep (2)没有出现错误,你可能需要等待元素。

标签: python google-chrome webdriver modal-dialog lettuce


【解决方案1】:

这是python语法

from selenium.webdriver.remote.webdriver import WebDriver

browser = WebDriver()

# do other stuff here

browser.switch_to_alert().accept()

# continue with other stuff here

警报 api 位于 selenium.webdriver.common.alert

【讨论】:

    【解决方案2】:

    以下代码使用 Java,您可以尝试使用以下代码将其转换为 Python 语法。对不起,我是 Webdriver - Java Tester,我不能给你 Python 代码。希望这能解决您的要求。

    Alert alertDialog = driver.switchTo().alert();
    //Get the alert text
    String alertText = alertDialog.getText();
    //Click the OK button on the alert.
    alertDialog.accept();
    

    干杯,

    马赫什

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多