簡體   English   中英

Webdriver等待driver.current.url

[英]Webdriver wait for driver.current.url

我有下面的代碼工作正常。 但是,當我嘗試從下面的代碼中刪除休眠時,出現斷言失敗錯誤。 有人可以建議我如何將WebDriverWait用於self.driver.current.url即用於驗證斷言。

ele = WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[""@aria-label='Add Device Model']")))
ele.click()    
sleep(5)    
self.assertEqual(True, ("adddevicemodel" in self.driver.current_url))

Java和C#已經為url實現了ExpectedConditions 我的猜測是直到Python趕上它只是一米的時間。 同時,您可以使用on實現

class wait_url_to_contain(object):
    def __init__(self, _text):
        self.text = _text

    def __call__(self, driver):
        return self.text in driver.current_url

wait = WebDriverWait(self.driver, 30)
ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[""@aria-label='Add Device Model']")))
ele.click()
wait.until(wait_url_to_contain("adddevicemodel"))
self.assertEqual(True, ("adddevicemodel" in self.driver.current_url))

正如Gaurang Shah評論中提到的那樣,此方法已於20176月13日實施,現已成為Selenium for Python的一部分。

您的示例的用法:

from selenium.webdriver.support.expected_conditions as EC

ele = WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[""@aria-label='Add Device Model']")))
ele.click()    

context.wait.until(EC.url_contains('adddevicemodel'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM