Reputation: 851
There are two possible xpath configurations for the html element
I'm working with.
I want to check if the first configuration is present for no more than 1 second. 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)
. 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. Does anyone know why it's behaving this way?
Upvotes: 1
Views: 2190
Reputation: 808
The answer can be found in the official documents, open this url and see the below content:
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.
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
Upvotes: 4