【问题标题】:How to print a log based on a driver current URL in Python如何在 Python 中基于驱动程序当前 URL 打印日志
【发布时间】:2021-05-10 03:31:51
【问题描述】:

目前正在从事一个小项目以掌握自动化的窍门,并且想知道一旦 url 重定向到某个页面,如何打印显示已登录的状态



if a is "https://pepsi.fooji.com/?#choose-address"
    print(f"[{datetime.now()}] - Logged in!")

【问题讨论】:

    标签: python selenium printing automation driver


    【解决方案1】:

    https://www.selenium.dev/selenium/docs/api/py/api.html

    https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html?highlight=current_url#selenium.webdriver.remote.webdriver.WebDriver.current_url

    if driver.current_url is "https://pepsi.fooji.com/?#choose-address"
        print(f"[{datetime.now()}] - Logged in!")
    

    参考python类,如果你参考webdriver远程类,你可以有一个内置的current_url属性,它将给出页面的当前url

    【讨论】:

    • 我还是一头雾水,我该怎么办?例如我需要检查条目的状态。我的代码如下 if "pepsi.fooji.com/?#unlucky" in driver.current_url: print(f"[{datetime.now()}] - Non Winner, retrying entry") if "pepsi.fooji.com/?#winner" in driver.current_url: print( f"[{datetime.now()}] - 获胜者!,检查您的电子邮件。") 但它没有打印当前状态。
    【解决方案2】:
    如果两个变量指向同一个对象,

    is 将返回 True。如果变量引用的对象相等,== 将返回 True

    您可以在Is there a difference between "==" and "is"?找到详细的相关讨论


    解决方案

    作为一种解决方案,您可以在通过driver.current_url 返回的字符串中验证字符串https://pepsi.fooji.com/?#choose-address,如下所示:

    if "https://pepsi.fooji.com/?#choose-address" in driver.current_url:
        print(f"[{datetime.now()}] - Logged in!")
        
    

    一种最佳方法验证通过driver.current_url 返回的字符串中的部分字符串choose-address 如下:

    if "choose-address" in driver.current_url:
        print(f"[{datetime.now()}] - Logged in!")
    

    【讨论】:

    • @Daniel 查看更新的答案并告诉我状态。
    • 知道了,但由于某种原因,当我在浏览器中完成赠品时,当前 URL 与我当前拥有的代码匹配。 if 'https://pepsi.fooji.com/?#unlucky' in driver.current_url: rint(f"[{datetime.now()}] - Non winner, retrying...")
    • 它没有打印当前状态
    • @Daniel Non winner, retrying... 听起来像是一个不同的问题。你能根据你的新要求提出一个新问题吗? Stackoverflow 贡献者将很乐意为您提供帮助。
    • @Daniel 好消息!!!请accept answer 点击已解决您的问题的answer 旁边的空心刻度线并upvote answers 这对你有帮助,对未来的读者有帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多