我试图在以下网站获得定时器的值:https://userinyerface.com/game.html计时器从零开始,但问题是,当我使用以下python代码检索带有selenium的计时器:
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()
将计时器打印为00:00:02,如果add time.sleep(1),则返回一个错误,因为页面在查找元素之前没有时间加载。如果我执行time.sleep(2),它返回00:00:02,我如何检查计时器是从00:00:00开始的?有没有办法找到那个特定元素的初始值?
我已经尝试使用显式等待,但无济于事。
您不需要也不应该硬编码暂停。
应选用WebDriverWait
expected_conditions
。
你需要等待的时刻,当页面加载和计时器元素是可见.
这时需要从那里取值。
下面的代码可以工作:
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:webdriverschromedriver.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)
输出为:
00:00:00
乌利希期刊指南您也可以使用XPath或CSS选择器定位计时器元素。如何定位元素不是问题,关键是使用WebDriverWait
expected_conditions
。
例如,您可以使用这一行:
initial_value = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='timer timer--white timer--center']"))).text
的Insteaqdinitial_value = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "timer"))).text
给出完全相同的结果。
获取定时器的初始值,即00:00:00从网站你需要诱导WebDriverWait为presence_of_element_located(),你可以使用以下任何一个定位策略:
-
使用CSS_SELECTOR和
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
-
使用CSS_SELECTOR和
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
-
使用CSS_SELECTOR和
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
-
使用CSS_SELECTOR和所有类:
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
-
使用visbility_of_element_located ()和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
-
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC