Python Selenium说这花的时间太长了
我正在使用的htmlPython Selenium说这花的时间太长了,python,selenium,Python,Selenium,我正在使用的html元素有两种可能的xpath配置 我想检查第一个配置是否存在不超过1秒。然后我想检查第二个配置是否存在。我就是这样做的: 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.supp
元素有两种可能的xpath配置
我想检查第一个配置是否存在不超过1秒。然后我想检查第二个配置是否存在。我就是这样做的:
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
主要问题是,当我显式地将其设置为wait=WebDriverWait(元素,1)
时,查找finalElement1
(在第一个try块中)的函数需要大约6秒的时间超时。我很困惑
我知道已经有很多关于这方面的内容,但由于某些原因,我无法让它发挥作用。有人知道它为什么会这样吗?答案可以在官方文件中找到,打开并查看以下内容:
警告:不要混合隐式和显式等待。这样做可能会导致不可预测的等待时间。例如,将隐式等待设置为10秒,显式等待设置为15秒可能会导致20秒后发生超时
如果您确实需要同时使用隐式和显式等待,您可以这样尝试:
browser.implicitly\u wait(0)这是因为:browser.implicitly\u wait(6)这是一个全局设置。另外,您不应该混合使用隐式和显式等待。隐式等待对于在此处加载页面的其余部分很重要。你能解释一下为什么把这两种方法混合起来是不好的做法吗?把它拿出来并没有解决我的问题,这正是问题所在。但是,我必须将其设置为新值才能生效。非常感谢。