1

Im trying to get the value of the timer in the following website: https://userinyerface.com/game.html The timer starts from zero, however the problem is that when I retrieve the timer with selenium using the following python code:

def setUp(self):
    self.driver = webdriver.Chrome()

def test_path(self):
    driver = self.driver
    driver.get("https://userinyerface.com/game.html")
    try:
        timer_is_displayed = WebDriverWait(driver,
        10).until(EC.visibility_of_element_located((By.XPATH, '//div//div[1]//div[2]//div[2]                                 //div')))
        timer = driver.find_element(By.XPATH, '//div//div[1]//div[2]//div[2]//div')
        print(timer.text)
    finally:
        driver.quit()

it prints the timer as 00:00:02, if add time.sleep(1) it returns an error as the page did not have time to load before looking for the element. If I do time.sleep(2) it returns 00:00:02, how can I check that the timer starts from 00:00:00? is there a way to find the starting value of that particular element?

I've tried using explicit waits to no avail.

  • Welcome Valente! Please take some time to read the introduction to Stack Overflow and earn your first badge. We're a little bit different from other sites. Here's how... https://stackoverflow.com/tour – user10186832 Jan 22 '23 at 18:36
  • Maybe this is because `driver.get` blocks until the page is fully loaded, at which point the timer has already advanced two seconds. Not sure if there is a solution for this. – Fractalism Jan 22 '23 at 19:16

2 Answers2

0

To get the initial value of the timer i.e. 00:00:00 from the website you need to induce WebDriverWait for the presence_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR and timer class:

    driver.get("https://userinyerface.com/game.html")
    print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.timer"))).text) #00:00:00
    
  • Using CSS_SELECTOR and timer--white class:

    driver.get("https://userinyerface.com/game.html")
    print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.timer--white"))).text) #00:00:00
    
  • Using CSS_SELECTOR and timer--center class:

    driver.get("https://userinyerface.com/game.html")
    print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.timer--center"))).text) #00:00:00
    
  • Using CSS_SELECTOR and all the classes:

    driver.get("https://userinyerface.com/game.html")
    print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.timer.timer--white.timer--center"))).text) #00:00:00
    
  • Using visibility_of_element_located() and CSS_SELECTOR:

    driver.get("https://userinyerface.com/game.html")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.timer.timer--white.timer--center"))).text) #00:00:00
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • When reading web element text content it's better to wait for element visibility, not just presence... – Prophet Jan 22 '23 at 20:20
  • @Prophet When the question is how quick you can reach to the destination, `presence` may be prefered over `visibility`. However you possibly missed the last option with _`visibility_of_element_located()`_ – undetected Selenium Jan 22 '23 at 20:46
  • OP asked how to validate the timer starts with `00:00:00`. Actually he want's to extract the element text value, as I do in my code. I actually run it and I actualy got that result – Prophet Jan 22 '23 at 21:32
  • @Prophet You mentioned _"Actually he want's to extract the element text value"_ and _"I actualy got that result"_: Is my solution doing something else? Did you execute my code? – undetected Selenium Jan 22 '23 at 22:18
  • I know, in most cases waiting for element presence will work. But still. If we want to extract the element text it is more correct to wait for elements visibility. And in case element should be clicked we should wait for element clickability. – Prophet Jan 23 '23 at 06:00
  • @Prophet Sounds great. Seems now you have learned the core difference between presence/visibility/interactibility. – undetected Selenium Jan 23 '23 at 08:50
  • It's not higher mathematics :) – Prophet Jan 23 '23 at 08:56
0

You do not need and should not hardcoded pauses.
WebDriverWait expected_conditions should be used.
You need to wait for the moment when the page is loaded and timer element is visible.
This is the moment when you need to take the value from there.
The following code works:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)

url = "https://userinyerface.com/game.html"
driver.get(url)
initial_value = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "timer"))).text
print(initial_value)

The output is:

00:00:00

UPD You could locate the timer element with XPath or CSS Selector as well. It's no the issue how to locate element, the point here is to use WebDriverWait expected_conditions.
For example you can use this line:

initial_value = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='timer timer--white timer--center']"))).text

Insteaqd of

initial_value = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "timer"))).text

It gives absolutely the same result.

Prophet
  • 32,350
  • 22
  • 54
  • 79