简体   繁体   English

Selenium-Python-如何减少NoSuchElementException的时间

[英]Selenium-Python - How to reduce time for NoSuchElementException

I have a test case where I need to check that on clicking an element, a popup is NOT displayed. 我有一个测试用例,需要检查单击某个元素时是否不显示弹出窗口。 This code works fine, but it takes too long, 60 seconds for NoSuchElementException, print the PASS condition and move to next test case. 该代码可以正常工作,但是NoSuchElementException花费的时间太长,需要60秒,打印PASS条件并移至下一个测试用例。 How can I reduce the wait time in this case? 在这种情况下如何减少等待时间?

driver.find_element_by_xpath(
".//*[@id='assetIdDIV']/div/myaots-input/div/div/div[1]/span/i[2]"
).click()

try:
    DUP_popup = driver.find_element_by_xpath(
    ".//*[@id='DuplicateTicketsPopup']/div/div/div/div[1]/span[2]/img"
    )

    if (DUP_popup):
        print ("Duplicate tkts popup is displayed - Fail")

except NoSuchElementException:
        print ("Duplicate popup not displayed - PASS")

You can use explicit wait. 您可以使用显式等待。

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully within timeout, otherwise throw TimeoutException WebDriverWait默认情况下每500毫秒调用一次ExpectedCondition,直到它在超时内成功返回为止,否则抛出TimeoutException

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
  DUP_popup = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPath, 
      "//*[@id='DuplicateTicketsPopup']/div/div/div/div[1]/span[2]/img"))
  )

  # This waits up to 10 seconds before throwing a TimeoutException 
  # unless it finds the element to return within 10 seconds.

  if (DUP_popup):
    print ("Duplicate tkts popup is displayed - Fail")

except TimeoutException:
  print ("Duplicate popup not displayed - PASS")

More detail about explicit wait and implicit wait can be found here 可以在此处找到有关显式等待和隐式等待的更多详细信息

It is not clear to why it would take as long as 60 seconds to raise NoSuchElementException and print the pass condition but you can configure the wait period through a waiter inducing WebDriverWait as follows : 目前尚不清楚为什么引发NoSuchElementException并打印通过条件会花费长达60秒的时间,但是您可以通过以下方法来配置等待时间,该等待时间由等待者来引发WebDriverWait ,如下所示:

driver.find_element_by_xpath(".//*[@id='assetIdDIV']/div/myaots-input/div/div/div[1]/span/i[2]").click()
if(len(WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, ".//*[@id='DuplicateTicketsPopup']/div/div/div/div[1]/span[2]/img"))))!= 0):
    print ("Duplicate tkts popup is displayed - Fail")
else:
    print ("Duplicate popup not displayed - PASS")

Note : Incase your program includes _implicitly_wait()_ you need to remove the instances of _implicitly_wait()_ as the documentation clearly mentions Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. 注意 :如果您的程序包含_implicitly_wait()_,则需要删除_implicitly_wait()_的实例,因为文档中明确提到Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.

Check your driver instance's implicit wait value. 检查驱动程序实例的隐式等待值。 If implicit value is set to 60secs, then it'll wait till 60secs to throw any selenium related exceptions. 如果隐式值设置为60secs,则它将等待60secs引发任何与硒相关的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM