Python Selenium web driver wait based on a condition of text in an element

180 views Asked by At

An HTML element with the id equity_timeStamp contains the date and time on the website I intend to scrape using selenium and Bs4. The text changes based on the date but the prefix As on and the suffix IST remains. Is there any way I can use text_to_be_present_in_element method to check for only presence of that prefix or suffix instead of the whole text as the date and time changes? Regex maybe? One way I found is to create a custom condition class but that's a little too much so is there an inbuilt way to do that?

Sample Text inside the element: As on 28-Aug-2020 15:30:00 IST

Code that I want to be helped with:

wait = WebDriverWait(driver, 10)
wait.until(expected_conditions.text_to_be_present_in_element((By.ID, 'equity_timeStamp'),'As on'))
1

There are 1 answers

0
Ahmed Mamdouh On

I wish that I see the page source of the website, but I'm wondering if you use the following command.

wait.until(expected_conditions.presence_of_all_elements_located((By.XPATH, '//*[@id="equity_timeStamp" and contains(text(), "As on") and contains(text(), "IST")]')))