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.
CodePudding user response:
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.
CodePudding user response:
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