简体   繁体   English

Selenium WebDriverWait 花费的时间太长

[英]Selenium WebDriverWait is taking too long

There are two possible xpath configurations for the html element I'm working with.我正在使用的 html element有两种可能的 xpath 配置。

I want to check if the first configuration is present for no more than 1 second .我想检查第一个配置是否存在不超过1 秒 Then I would like to check if second configuration is present.然后我想检查是否存在第二个配置。 This is how I am doing it:这就是我的做法:

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

browser.get(URL)
browser.implicitly_wait(6)

element = browser.find_elements_by_xpath("/html/body/div[4]/div[3]/div[1]/ ......")


try:
    time1 = time.time()
# The following 2 lines are taking too long:
    wait = WebDriverWait(element, 1)
    finalElement1 = wait.until(EC.presence_of_element_located((By.XPATH, ".//div[@class='classNAME']/a/header/div[@class='OtherClassName']/span[@class='FinalClassName']"))).text
    print("First element took (seconds) to find :" + str((time.time()-time1)))
# This prints around 0.02 seconds
    except (TimeoutException, Exception):
         print("Took this amount of seconds to timeout: "+ str((time.time()-time1)))
# This prints around 6 seconds 
         try:
              time1 = time.time()
              tempElement = element.find_element_by_xpath(".//div[@class='_0xLoFW _78xIQ- EJ4MLB JT3_zV']/a/header/div[@class='_0xLoFW u9KIT8 _7ckuOK']")
               finalElement1 = tempElement.find_element_by_xpath(".//span[@class='u-6V88 ka2E9k uMhVZi dgII7d z-oVg8 _88STHx cMfkVL']").text
               finalElement2 = tempElement.find_element_by_xpath(".//span[@class='u-6V88 ka2E9k uMhVZi FxZV-M z-oVg8 weHhRC ZiDB59']").text
               print("Second element took (seconds) to find : "+ str((time.time()-time1)))
# This prints around 0.08 seconds
               except:
                    print("None of the above")
                    continue
               pass

The main issue is that the function which looks for finalElement1 (in the first try block) takes around 6 seconds to time out when I explicitly set it to wait = WebDriverWait(element, 1) .主要问题是,当我将它显式设置为wait = WebDriverWait(element, 1)时,查找finalElement1 (在第一个 try 块中)的函数需要大约 6 秒才能超时。 I'm confused我糊涂了

I know there is already a lot of content on SO and on the selenium blog about this, but for some reason I can't get it to work.我知道在 SOselenium 博客已经有很多关于此的内容,但由于某种原因我无法让它工作。 Does anyone know why it's behaving this way?有谁知道它为什么会这样?

The answer can be found in the official documents, open this url and see the below content:答案可以在官方文档中找到,打开这个url ,看到如下内容:

Warning: Do not mix implicit and explicit waits.警告:不要混合隐式和显式等待。 Doing so can cause unpredictable wait times.这样做可能会导致不可预测的等待时间。 For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.例如,设置 10 秒的隐式等待和 15 秒的显式等待可能会导致在 20 秒后发生超时。

If you really need to use implicit and explicit waits at the same time, you could try like this:如果你真的需要同时使用隐式和显式等待,你可以尝试这样:

browser.implicitly_wait(0)   <-- set implicit wait to the default value 0 before explicit waits

finalElement1 = wait.until(EC.presence_of_element_located((By.XPATH, ".//div[@class='classNAME']/a/header/div[@class='OtherClassName']/span[@class='FinalClassName']"))).text

browser.implicitly_wait(6)   <-- set implicit wait to the value you need after explicit waits

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

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