0
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'
4

3 回答 3

1

你需要实例化WebDriverWait

wait = WebDriverWait(webdriver, 5)

你可以使用wait对象。

于 2018-07-19T09:51:36.213 回答
0

根据您的代码试验,我仍然不确定您的用例以及您要使用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,则可以使用以下解决方案:

  • 要验证登录成功/失败,您可以检查包含 Managerhomepage 的新URL包含Guru99 Bank Manager HomePage的新页面标题。因此,您可以将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
    
于 2018-07-19T11:01:04.753 回答
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)

更多信息可以在这里找到。

于 2018-07-19T09:48:00.740 回答