0

this below is the FULL html element I grabbed from the web

<div data-v-a27cb890="" class="text-2xl font-bold font-numeric">19.60</div>

as you notice, I am selecting a 20 second countdown html timer code, The problem here is the code I wrote below doesn't seem to work. As the timer hits 15.28 seconds. I want to print it out.

timer = WebDriverWait(driver, 30).until(EC.text_to_be_present_in_element(By.XPATH, '//*[@id="page-scroll"]/div[1]/div/div/div[3]/div[3]/div/div[2]'), "15.28")
print(float(timer.text))

after WebDriverWaiting for 30 seconds it cant find the text I wanted to print out even the timer rolls past on my screen. Then it gives an TimeoutException error.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

According to selenium.webdriver.support.wait doc, the default poll interval is 0.5s. So it might not match your expectation that can catch 15.28s. Try to customize your own interval:

WebDriverWait webDriverWait = new WebDriverWait(driver, 30);
webDriverWait.pollingEvery(Duration.ofMillis(50)); // 50 milliseconds
Eric Wei
  • 61
  • 6
0

According to the definition, text_to_be_present_in_element() should be called within a tuple as it is not a function but a class, where the initializer expects just 1 argument beyond the implicit self:

class text_to_be_present_in_element(object):
    """ An expectation for checking if the given text is present in the
    specified element.
    locator, text
    """
    def __init__(self, locator, text_):
    self.locator = locator
    self.text = text_

    def __call__(self, driver):
    try:
        element_text = _find_element(driver, self.locator).text
        return self.text in element_text
    except StaleElementReferenceException:
        return False

So instead of:

timer = WebDriverWait(driver, 30).until(EC.text_to_be_present_in_element(By.XPATH, '//*[@id="page-scroll"]/div[1]/div/div/div[3]/div[3]/div/div[2]'), "15.28")

You need to (add an extra parentheses):

timer = WebDriverWait(driver, 30).until(EC.text_to_be_present_in_element((By.XPATH, '//*[@id="page-scroll"]/div[1]/div/div/div[3]/div[3]/div/div[2]'), "15.28"))

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352