📅  最后修改于: 2023-12-03 14:56:30.563000             🧑  作者: Mango
在使用Selenium和Python进行自动化测试时,经常涉及到时间和日期的操作,如等待页面加载、判断元素是否存在、模拟用户操作等,这时就需要使用到时间的相关操作。
获取当前时间有多种方法,下面是其中几种:
datetime
模块进行获取from datetime import datetime
now = datetime.now()
print(now)
WebDriverWait
和expected_conditions
模块进行获取from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
now = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "su")))
print(now.text)
from datetime import datetime
time1 = datetime.now()
time2 = datetime.now()
if time1 == time2:
print("时间相等")
else:
print("时间不相等")
from datetime import datetime
time1 = datetime.now()
time2 = datetime.now()
if time1 > time2:
print("time1比time2大")
elif time1 < time2:
print("time1比time2小")
else:
print("time1和time2相等")
strftime
方法进行输出from datetime import datetime
now = datetime.now()
nowStr = now.strftime('%Y-%m-%d %H:%M:%S.%f')
print(nowStr)
Markdown格式如下:
## 硒Python中相同时间的条件
在使用Selenium和Python进行自动化测试时,经常涉及到时间和日期的操作,如等待页面加载、判断元素是否存在、模拟用户操作等,这时就需要使用到时间的相关操作。
### 获取当前时间
获取当前时间有多种方法,下面是其中几种:
- 使用Python内置的`datetime`模块进行获取
```python
from datetime import datetime
now = datetime.now()
print(now)
WebDriverWait
和expected_conditions
模块进行获取from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
now = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "su")))
print(now.text)
from datetime import datetime
time1 = datetime.now()
time2 = datetime.now()
if time1 == time2:
print("时间相等")
else:
print("时间不相等")
from datetime import datetime
time1 = datetime.now()
time2 = datetime.now()
if time1 > time2:
print("time1比time2大")
elif time1 < time2:
print("time1比time2小")
else:
print("time1和time2相等")
strftime
方法进行输出from datetime import datetime
now = datetime.now()
nowStr = now.strftime('%Y-%m-%d %H:%M:%S.%f')
print(nowStr)