0

I am writing a script that is constantly looping and will need to stop when it detects that there is text in a certain element on the page. I couldn't find any help in the selenium python docs about doing this but there must be a way. In case you still don't understand what I need to do, here is some code:

While #element is not visible:
   #do stuff
#when element is visible break out of statement and do something else

EDIT: I know the exact text and the exact xpath of the element i am looking for. However, I am just looking for a general response so I don't mind an answer lacking detail.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Faizan Shah
  • 141
  • 16
  • Your issue is not quite clear. Do you know which element exactly should contain text? Do you know the complete text or only partial? How long can it take to wait for required text? Add more details to your question – JaSON Oct 30 '20 at 11:47
  • @JaSON ok i will edit – Faizan Shah Oct 30 '20 at 12:17
  • Please have a look at this Problem: [Similar Problem](https://stackoverflow.com/questions/69429394/variable-value-dont-get-update-in-while-loop-with-python-selenium) – Idrs Oct 03 '21 at 22:41

5 Answers5

0

You can try text_to_be_present_in_element expected condition and implement ExplicitWait as below:

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()
driver.get(URL)

timeout = <SET MAXIMUM AMOUNT OF TIME IN SECONDS TO WAIT> 
your_text = '<REQUIRED TEXT>'

element = WebDriverWait(driver, timeout).until(EC.text_to_be_present_in_element((By.ID, "<ElementID>"), your_text))

This will return element as soon as required text appeared

JaSON
  • 4,843
  • 2
  • 8
  • 15
  • The OP's code is showing that he wants to execute some code while looping. This approach won't allow for that. – JeffC Oct 30 '20 at 14:29
0

The code below will get the desired element and then loop until it is displayed (visible). At the end of the loop, you will probably want to refetch the element (in case the page changes) before you check if it's displayed at the top of the loop.

element = driver.find_element_by_XXX(locator)
while not element.is_displayed():
    #do stuff
    element = driver.find_element_by_XXX(locator)
#when element is visible break out of statement and do something else

I would also add that you probably want to have a loop escape just in case the element never becomes visible. You would do that by getting the current time before entering the loop and then have a check in the while condition that checks to make sure that it hasn't been more than say 30s since the start time. That way your script doesn't get stuck in an endless loop.

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

To continue in a loop and stop when Selenium detects that there is text in a certain element on the page you can use the following Locator Strategy:

while True:
    try:
        WebDriverWait(driver, 5).until(EC.invisibility_of_element((By.XPATH, "element_xpath")))
        #do stuff
        continue
    except:
        break
#do something else
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

I am coding in C# for Selenium and never actually used this in a while loop but I am using implicit wait to make my tests to wait until the element I am looking for is available. The function is quite easy to write but I am not sure if this will work for you.

_ = driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

Basically what this does is Selenium waits until the element is available or 30 seconds in total and then it fails the test if the element is not available.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • That's not what this code does. Your posted code sets a one-time timeout for the driver instance to 30 seconds. Calling it over and over again doesn't actually do anything. It's not actually waiting when you call it. Implicit waits are also a bad practice according to the Selenium contributors. – JeffC Oct 30 '20 at 14:18
  • This comment is also not in Python, which is the language OP is working in according to the post – hanreli Mar 03 '21 at 20:01
-2

Try the below code, this'll surely help you.

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'/path/chromedriver')
driver.get('https://example.com')
condition = driver.find_element_by_<METHOD>('<|Element|>')
while not condition:
     <<..yourWork..>>
  • This code doesn't take into account what the OP asked for... while the element is not visible do stuff. Your "condition" checks for null, not visible. – JeffC Oct 30 '20 at 14:30