2

I'm having trouble understanding what are the differences between these two blocks of codes. Sending clicks both work in webdriverwait and find_elements.

Code 1

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

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 45)


wait.until(EC.presence_of_element_located((By.LINK_TEXT, "ABC"))).click()

Code 2

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

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 45)

wait.until(EC.presence_of_element_located((By.LINK_TEXT, "ABC")))
driver.find_element(By.LINK_TEXT,"ABC").click()
    
1
  • it is the same thing, presence_of_element_located() returns the element (if found), the second code is redundantly finding the element again. Commented Jul 31, 2024 at 22:12

1 Answer 1

0
wait.until(EC.presence_of_element_located((By.LINK_TEXT, "ABC")))

This will return a webElement and you can call methods which are available for webElement i.e click(), send_keys(), clear() etc.

driver.find_element(By.LINK_TEXT,"ABC")

This also does the same thing i.e: it will return a webElement.

Difference: The main difference between these two: WebDriverWait is an explicit wait in Selenium that means if element is not found in DOM then it will try again after 500ms which is .5 sec again until 45 sec (since you've defined 45 sec in the constructor call). if found within this time it will return the webElement, otherwise it will raise timeOutException.

On the other hand driver.find_element will try to find the element immediately if found it will return the webElement if not, exception will be raised either NoSuchElement or some other.

You can read more about explicit wait here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.