Selenium Wait - Explicit Wait For Element Text Value
Waiting in selenium can be done in different ways. In this tutorial, we will use the explicit wait
functionality for waiting a text from value attribute of an element.
But before that, please make sure you have read the
first blog on this
series to do the prerequisites.
Selenium Explicit Wait For a Element Text
- Create a file seleniumwaittextvalue.py and paste the following codes
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
from selenium.common.exceptions import TimeoutException
from datetime import datetime
The codes above imports the required library that we will use.
- Add this line
driver = webdriver.Firefox(executable_path="geckodriver.exe")
The code above will create a webdriver instance for Firefox.
- Add this line
driver.get("https://slackingslacker.github.io/seleniumindex#/seleniumwait")
The line will got to the website (https://slackingslacker.github.io/seleniumindex#/seleniumwait).
- Add this function as is
def wait_for_text(wait_time: int, selector: str, text_value: str):
try:
print("[{}] Finding text {}".format(str(datetime.now()), selector))
WebDriverWait(driver, wait_time).until(
EC.text_to_be_present_in_element_value((By.CSS_SELECTOR, selector), text_value)
)
print("[{}] Text found".format(str(datetime.now())))
except TimeoutException as e:
print("[{}] Text did not load".format(str(datetime.now())))
This method will wait for the elements value attribute text. It will print a message if the element text loaded
or not.
- Add this line
wait_for_text(3, "div#textExpectation > div:nth-of-type(1)", "This is an input")
This line will call the method we created and wait for 3 seconds until it gives an error.
- Add this line
wait_for_text(6, "div#textExpectation > div:nth-of-type(2) > input", "This is an input")
This line will call the method we created and will display Text found.
- Add this line
wait_for_text(9, "div#textExpectation > div:nth-of-type(1) > input", "this is an input")
Again will call the method we created and wait for 9 seconds until it gives an error.
<div id="textExpectation">
<div>Expected Text</div>
<div><input type="text" value="This is an input"></div>
</div>
The above HTML is taken from the website.
- Add this line
driver.close()
The line will close the webdriver as well as the browser.
- Run the seleniumwaittextvalue.py. It should do the following:
- Open the firefox browser
- Browser goes to https://slackingslacker.github.io/seleniumindex#/seleniumwait
- Call the Method 3 times which prints messages in the console
- Closes the browser
Program Sample Output
[2020-06-04 23:09:35.289359] Finding text div#textExpectation > div:nth-of-type(1)
[2020-06-04 23:09:38.512822] Text did not load
[2020-06-04 23:09:38.512822] Finding text div#textExpectation > div:nth-of-type(2) > input
[2020-06-04 23:09:38.543957] Text found
[2020-06-04 23:09:38.543957] Finding text div#textExpectation > div:nth-of-type(1) > input
[2020-06-04 23:09:47.588846] Text did not load
Output explanations
- The code looks for an element given the CSS selector div#textExpectation > div:nth-of-type(1) and checks the text for that element
- The code does not find the element within 3 seconds
- The code looks for an element given the CSS selector div#textExpectation > div:nth-of-type(2) and checks the text for that element
- The code found the text
- The code looks for an element given the CSS selector div#textExpectation > div:nth-of-type(1) checks the text for that element
- The code does not find the element within 9 seconds
<div><input type="text" value="This is an input"></div>
The matching of the text is case sensitive so the text does not match.
As you may have noticed that the waiting time varies to what you have supplied to the
WebDriverWait class.
Final Selenium Code
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
from selenium.common.exceptions import TimeoutException
from datetime import datetime
driver = webdriver.Firefox(executable_path="geckodriver.exe")
driver.get("https://slackingslacker.github.io/seleniumindex#/seleniumwait")
def wait_for_text(wait_time: int, selector: str, text_value: str):
try:
print("[{}] Finding text {}".format(str(datetime.now()), selector))
WebDriverWait(driver, wait_time).until(
EC.text_to_be_present_in_element_value((By.CSS_SELECTOR, selector), text_value)
)
print("[{}] Text found".format(str(datetime.now())))
except TimeoutException as e:
print("[{}] Text did not load".format(str(datetime.now())))
wait_for_text(3, "div#textExpectation > div:nth-of-type(1)", "This is an input")
wait_for_text(6, "div#textExpectation > div:nth-of-type(2) > input", "This is an input")
wait_for_text(9, "div#textExpectation > div:nth-of-type(1) > input", "this is an input")
driver.close()
Conclusion
Waiting time in selenium can be used for waiting elements value attribute text.