0

How do I have Selenium wait until a certain element by ID is present on the page?

driver.find_element(By.ID, "FirstName").send_keys("MyFirstName")
driver.find_element(By.ID, "LastName").send_keys("MyLastName")
driver.find_element(By.ID, "PhoneNumber").send_keys("myPhoneNumber")
driver.find_element(By.ID, "Email").send_keys("myEmail")

For instance line 3, might not be present on the page I want it to wait until the element is present, then do the certain task and proceed with the next line of the code.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
user901
  • 35
  • 3

2 Answers2

1

You can use this.

Java:

WebDriverWait w1 = new WebDriverWait(driver, 5);
w1.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit_btn")));

Python:

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.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

See more on: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

Gaj Julije
  • 646
  • 4
  • 24
  • End of the code, what if I don't want to have driver.quit()? What should be changed? Because I would like to continue on to the next line of codes after the element is present – user901 Feb 01 '21 at 07:47
  • Than you can just add log, for e.g. print("Web Element with ID: 123 is not present") and sam logical variable isPresent= false; And after you can add any code that you want which will not go under finally block. Be aware that after you can not interact with element that is not present. – Gaj Julije Feb 01 '21 at 07:53
  • 1
    How long does it wait for the element to be present before continuing on to the next block? – user901 Feb 01 '21 at 07:56
  • Depands on every particular case. Usualy you dont need more that 5 seconds, but it depands as I said. For e.g you can upload a larg file, and after uploading you get message "File is uploaded", in that case you could wait for few min. :-) – Gaj Julije Feb 01 '21 at 07:59
  • 1
    I mean the code you specified try: element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "myDynamicElement")) how long does it wait for the element to be present? 10 seconds? – user901 Feb 01 '21 at 08:00
  • Yes, 10 seconds. – Gaj Julije Feb 01 '21 at 08:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228097/discussion-between-gaj-julije-and-user901). – Gaj Julije Feb 01 '21 at 08:05
  • Thanks, the code worked just had to adjust the timeout. – user901 Feb 01 '21 at 09:21
0

To wait until the element is present to send a character sequence you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using ID:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "PhoneNumber"))).send_keys("user901")
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#myPhoneNumber"))).send_keys("user901")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='myPhoneNumber']"))).send_keys("user901")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217