尝试/捕获块显示TypeError:直到not_not()缺少1个必需的位置参数:“方法”

时间:2018-07-19 09:38:07

标签: python selenium selenium-webdriver webdriver webdriverwait

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import autoit
wait = WebDriverWait
firefox_options = webdriver.FirefoxOptions()
firefox_options.set_preference("dom.webnotifications.enabled", False)
driver = webdriver.Firefox(firefox_options=firefox_options, executable_path=r'C:\\firefoxdriver\\geckodriver.exe')
driver.maximize_window()
driver.get("http://www.demo.guru99.com/V4/")
driver.find_element_by_xpath('/html/body/form/table/tbody/tr[1]/td[2]/input').send_keys('mngr253724')
driver.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td[2]/input').send_keys('Et54Uje')
driver.find_element_by_xpath('/html/body/form/table/tbody/tr[3]/td[2]/input[1]').click()
time.sleep(5)

try:
    page_loaded = wait.until_not(
        lambda driver: driver.current_url == "http://www.demo.guru99.com/V4/manager/Managerhomepage.php"
    )
except TimeoutException:
    driver.fail("Loading timeout expired")

driver.assertEqual(
    driver.current_url,
    correct_page,
    msg="Successful Login"
)

错误:

File "C:/Users/Dell/PycharmProjects/Test_Order/Test_Order.py", line 27, in <module>
lambda driver: driver.current_url == "http://www.demo.guru99.com/V4/manager/Managerhomepage.php"
TypeError: until_not() missing 1 required positional argument: 'method'

3 个答案:

答案 0 :(得分:0)

替换:

try:
    page_loaded = wait.until_not(
        lambda driver: driver.current_url == "http://www.demo.guru99.com/V4/manager/Managerhomepage.php"
    )
except TimeoutException:
    driver.fail("Loading timeout expired")

具有:

try:
    page_loaded = wait.until_not(EC.url_to_be("http://www.demo.guru99.com/V4/manager/Managerhomepage.php"))
except TimeoutException:
    driver.fail("Loading timeout expired")

注意: ,您必须进行一些导入:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

PS: ,您必须正确定义wait

wait = WebDriverWait(webdriver, 10)

更多信息here

答案 1 :(得分:0)

您需要实例化WebDriverWait

wait = WebDriverWait(webdriver, 5)

您可以使用wait对象。

答案 2 :(得分:0)

根据您的代码试用,我仍然不确定您的用例以及为什么要将{em> until 条件用作until_not的原因,如下所示:

page_loaded = wait.until_not(lambda driver: driver.current_url == "http://www.demo.guru99.com/V4/manager/Managerhomepage.php")

如果您的用例是要验证应用程序是否使用有效/错误的凭据将用户带到 url http://www.demo.guru99.com/V4/manager/Managerhomepage.php的页面,则可以使用以下解决方案:

  • 要验证登录成功/失败,您可以检查新的 URL 包含 Managerhomepage 还是新的 Page Title 包含 Guru99银行经理主页。因此,您可以将 WebDriverWait 期望条件设置为url_contains()title_contains(),如下所示:
  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    from selenium.common.exceptions import UnexpectedAlertPresentException
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("http://www.demo.guru99.com/V4/")
    driver.find_element_by_xpath('/html/body/form/table/tbody/tr[1]/td[2]/input').send_keys('mngr144003')
    driver.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td[2]/input').send_keys('bUtymYr')
    driver.find_element_by_xpath('/html/body/form/table/tbody/tr[3]/td[2]/input[1]').click()
    try:
                WebDriverWait(driver, 10).until(EC.url_contains("Managerhomepage"))
            #WebDriverWait(driver, 10).until(EC.title_contains("Manager"))
            print("Login is successful. Page Title is : %s" %driver.title)
    except (TimeoutException,UnexpectedAlertPresentException):
            print("Loading timeout expired")
    driver.quit()
    
  • 控制台输出(成功登录):

    Login is successful. Page Title is : Guru99 Bank Manager HomePage
    
  • 控制台输出(登录失败):

    Loading timeout expired