【问题标题】:Selenium with if-else statements not working带有 if-else 语句的 Selenium 不起作用
【发布时间】:2021-11-11 02:29:56
【问题描述】:

我正在尝试在特定时间使用 selenium 打开网页,但它不起作用。这是我的代码:

import time
from selenium import webdriver
from datetime import datetime

while True:
    PATH = "chromedriver.exe"
    driver = webdriver.Chrome(PATH)
    ct = datetime.now().strftime("%H:%M")

    if ct == 8:55:
        driver.get("https://webpage.com/")
    else:
        print("Waiting for time to be 8:55...")
        time.sleep(100)

预期:如果当前时间是 8:55,则必须打开网页“https://webpage.com/”,否则应打印文本“Waiting for the time to be 8:55”和睡眠 100 秒。

实际结果:代码返回这些错误:

Expected expression. Pylance [11, 19]
Type annotation not supported for this type of expression. Pylance [11, 19]
Unexpected indentation. Pylance [12, 9]
Unindent not expected. Pylance [15, 24]
Expected expression. Pylance [15, 24]
Statements must be separated by newlines or semicolons. Pylance [15, 24]

我使用 Visual Studio Code 作为我的 IDE,并且我安装了 selenium。

【问题讨论】:

    标签: python selenium if-statement pylance


    【解决方案1】:

    if ct == 8:55: 是语法错误。你可能想写

    if ct == "08:55":
    

    附带说明,如果您在 08:54:50 上睡了 100 秒,那么当您醒来时将是 08:56:30,您会错过窗口。你应该睡不到 1 分钟。

    【讨论】:

    • 哦,对不起,我很愚蠢——我忘了​​在driver.get("") 声明中添加一个链接。现在工作正常。我还在else 语句中添加了driver.quit(),并且我将time.sleep(25) 替换为time.sleep(100),这样它就不会开始向打开的窗口发送垃圾邮件。
    • 现在可以使用了,谢谢。
    猜你喜欢
    • 2022-11-29
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多