繁体   English   中英

Python For 循环打印语句

[英]Python For Loop Print statement

from selenium import webdriver
import time


def test_setup():
    global driver
    driver = webdriver.Chrome(executable_path="C:/ChromeDriver/chromedriver.exe")
    driver.implicitly_wait(5)
    driver.maximize_window()
    time.sleep(5)


    siteUrls = ["https://www.espncricinfo.com/", "https://www.t20worldcup.com/","https://www.iplt20.com/"]

    for url in siteUrls:
        openSite(url)

def openSite(siteUrl):
    driver.get(siteUrl)
    time.sleep(5)
    print("ESPN website is launched successfully")


def test_teardown():
    driver.close()
    driver.quit()

以上是我的代码,它运行得非常好,我的问题是它打印出与所有 3 个 URL 的输出相同的语句,但我希望它打印 3 个不同的语句

例如 - 我想要下面的预期输出

ESPN website is launched successfully
IPL website is launched successfully
world-cup site is launched successfully

But, currently I get output as below ( same statement repeated 3 times)
ESPN website is launched successfully
ESPN website is launched successfully
ESPN website is launched successfully

您需要提供一个适当的名称作为openSite的第二个参数。 例如,

    ...

    siteUrls = [
        ("ESPN", "https://www.espncricinfo.com/"),
        ("world-cup", "https://www.t20worldcup.com/"),
        ("IPL", "https://www.iplt20.com/")
    ]

    for name, url in siteUrls:
        openSite(name, url)


def openSite(name, siteUrl):
    driver.get(siteUrl)
    time.sleep(5)
    print(f"{name} website is launched successfully")

您的打印语句没有任何论据。 这就是为什么你总是得到相同的输出。 这是一个可能的解决方案:

def openSite(siteUrl):
    driver.get(siteUrl)
    time.sleep(5)
    print(siteUrl, "is launched successfully")
def openSite(siteUrl):
    driver.get(siteUrl)
    time.sleep(5)

    # Split the url at the period and get index 1 from list that contains site name
    site_name = siteUrl.split('.')[1]
    print(site_name + " website is launched successfully")

#output:
#>> espncricinfo website is launched successfully
#>> t20worldcup website is launched successfully
#>> iplt20website is launched successfully

您需要将某些内容传递给您的打印语句。 例如

print(f"{siteUrl} launched")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM