Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 警报_的函数为_present(),并将_切换到_alert()_Python_Selenium Webdriver_Alert - Fatal编程技术网

Python 警报_的函数为_present(),并将_切换到_alert()

Python 警报_的函数为_present(),并将_切换到_alert(),python,selenium-webdriver,alert,Python,Selenium Webdriver,Alert,我有代码(python/selenium): 第一行在浏览器中等待警报,第二行按“取消”按钮关闭此窗口。它很好用。我决定创建两个函数 def alertIsPresent(self, timeout=10): WebDriverWait(self.driver, timeout).until(EC.alert_is_present()) def alertDismiss(self): alert = self.driver.switch_to_a

我有代码(python/selenium):

第一行在浏览器中等待警报,第二行按“取消”按钮关闭此窗口。它很好用。我决定创建两个函数

    def alertIsPresent(self, timeout=10):
        WebDriverWait(self.driver, timeout).until(EC.alert_is_present())

    def alertDismiss(self):
        alert = self.driver.switch_to_alert()
        alert.dismiss()
我称之为函数:

    PageObject.alertIsPresent()
    PageObject.alertDismiss()
最后一个“)”带下划线,因为“参数自身未填充”。。。 我是python的新手,你能给我一些建议吗

pageObjectClass:

class PageObject:

def __init__(self, driver, xpathLocator):
    self.driver = driver
    self.locator = xpathLocator
    self.wait = WebDriverWait(self.driver, 100)

def waitElementToBePresent(self, timeout=10):
    WebDriverWait(self.driver, timeout).until(
        EC.visibility_of_element_located((By.XPATH, self.locator)))

def elementIsPresent(self):
    return EC.visibility_of_element_located((By.XPATH, self.locator))

def alertIsPresent(self, timeout=10):
    WebDriverWait(self.driver, timeout).until(EC.alert_is_present())

def alertDismiss(self):
    alert = self.driver.switch_to_alert()
    alert.dismiss()

您没有提到的客户端的版本信息

然而,根据目前的实施情况和:

因此,实际上,您需要替换该行:

alert = self.driver.switch_to_alert()
与:


无需使用两个函数,
EC.alert\u is\u present()
将返回警报

def alertIsPresent(self, timeout=10):
    return WebDriverWait(self.driver, timeout).until(EC.alert_is_present())
您还需要从类实例而不是类型调用它

PageObject(driver).alertIsPresent().dismiss()

您可以共享pageObject类吗?添加/更新有问题的对象。您可以尝试在其他类中创建对象,如
pageObject=pageObject()
,然后
pageObject.AlertDisclesh()
,如果我在第一个类中添加此对象,我有一些警告:
参数'driver'未填充参数'xpathLocator'未填充
但我不使用它。嗯。。。如果我删除“self”,我收到消息:
未解析的引用“self”
@Igor Potapov对
self
没有任何混淆,我错过了
驱动程序,更正了它。让我知道情况
alert = self.driver.switch_to.alert
def alertIsPresent(self, timeout=10):
    return WebDriverWait(self.driver, timeout).until(EC.alert_is_present())
PageObject(driver).alertIsPresent().dismiss()