【问题标题】:why is my if else statement returning the wrong result?为什么我的 if else 语句返回错误的结果?
【发布时间】:2021-12-06 12:20:33
【问题描述】:

我是 Python 和自动化的新手。 我们的网站在页面顶部有一个星期五的倒计时。我正在尝试查找该元素并验证它是否根据日期显示正确的数字和文本。

这是我的代码(默认为星期二的计数器不正确 - 即使是星期二)。所以我添加了打印语句来查看它提取了哪些数据,以及数字和文本是否正确,即使它首先列出了数字?

if dt.datetime.today().weekday() == 0:
    countdown_text = driver.find_element_by_class_name("friday-text").text
    countdown_number  = driver.find_element_by_class_name("count")
    if countdown_text == " days until #friday" and countdown_number == '4':
        print("Friday Counter is correct. It's Monday - unfortunately.")
    else:
        print("Friday Counter is incorrect for Monday.")

elif dt.datetime.today().weekday() == 1:
    countdown_text = driver.find_element_by_class_name("friday-text").text
    print(countdown_text)
    countdown_number  = driver.find_element_by_class_name("count")
    print(countdown_number)
    if countdown_text == "DAYS UNTIL #FRIDAY" or countdown_number == 3:
            print("Friday Counter is correct. It's Tuesday.")
    else:
        print("Friday Counter is incorrect for Tuesday.")

这是我的输出。

3
DAYS UNTIL #FIREBALLFRIDAY
<selenium.webdriver.remote.webelement.WebElement (session="9ee10b8bab4a7522f7e601d07d077e90", element="903e625a-5552-4500-a7fe-a7efca90666c")>
Fireball Friday Counter is incorrect for Tuesday.

【问题讨论】:

  • countdown_number 是一个 DOM 元素,而不是一个数字。
  • 如果将.text 添加到分配中,它将是一个字符串,而不是一个数字。
  • 为什么在第一个块中使用and,而在第二个块中使用or。第一个查找小写的#friday,第二个查找大写的#FRIDAY
  • 所以你可能想要countdown_number.text == '3':
  • @barmar 我现在专注于星期二,一旦我开始工作,我将修复第一个块以匹配。就使用或我试图隔离变量之一是否有效而另一个无效。至于大写/小写,html中的元素是小写的,css有文本转换,所以屏幕显示大写我都试过了,都没有用。将 .text 添加到倒计时数字,现在输出为 3 DAYS UNTIL #FIREBALLFRIDAY 3 Fireball Friday Counter 周二不正确。所以不确定第一个/额外的 3 来自哪里

标签: python selenium-webdriver automation


【解决方案1】:

知道了。我注释掉了 print countdown_number 以查看是否可以隔离额外的 3,并发现 countdown_text 将数字和文本分别提供给我。所以我更改了预期的文本并收到了正确的回复。

elif dt.datetime.today().weekday() == 1:
    countdown_text = driver.find_element_by_class_name("friday-text").text
    print(countdown_text)
    countdown_number  = driver.find_element_by_class_name("count").text
    #print(countdown_number)
    if countdown_text == "3 \nDAYS UNTIL #FRIDAY" or countdown_number == '3':
            print("Friday Counter is correct. It's Tuesday.")
    else:
        print("Friday Counter is incorrect for Tuesday.")

输出:

3
DAYS UNTIL #FIREBALLFRIDAY
Fireball Friday Counter is correct. It's Tuesday.

所以看起来我可以摆脱倒数部分,我的最终代码和输出将是

elif dt.datetime.today().weekday() == 1:
    countdown_text = driver.find_element_by_class_name("fireball-friday-text").text
    if countdown_text == "3\nDAYS UNTIL #FIREBALLFRIDAY":
            print("Fireball Friday Counter is correct. It's Tuesday.")
    else:
        print("Fireball Friday Counter is incorrect for Tuesday.")

输出

Fireball Friday Counter is correct. It's Tuesday.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 2020-07-30
    • 2021-08-31
    相关资源
    最近更新 更多