from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from time import sleep,ctime from datetime import datetime # 获得当前的时间datetime.now()可以计算出耗时 def wait(loc): try: sta=datetime.now() print(f"开始找元素,当前时间{sta}") ele=driver.find_element(By.CSS_SELECTOR,loc) ele.send_keys("1") driver.implicitly_wait(3) ele.send_keys(Keys.ENTER) except: print("没找到元素") finally: end=datetime.now() print(f"找元素的结束时间{end}") t=end-sta print(f'找元素耗时{t}') if __name__ == '__main__': driver=webdriver.Chrome() driver.get('https://www.baidu.com') loc='input#kw' driver.implicitly_wait(3) print("已经打开百度浏览器,等待3秒") wait(loc) sleep(3) print("回车后,等待3秒")
标签:__,loc,捕获,driver,datetime,耗时,print,import,异常 From: https://www.cnblogs.com/haha1988/p/17755396.html