如何调用名称以数字分隔的函数?从t1到t20 Python调用函数的示例?

2024-10-02 08:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

def t17():
    Clientbox = 'body > table:nth-child(6) > tbody > tr > td:nth-child(2) > form 
    > table > tbody > tr:nth-child(1) > td:nth-child(1) > table > tbody > 
    tr:nth-child(7) > td:nth-child(3) > table > tbody > tr:nth-child(1) > 
    td:nth-child(2) > select'
    Clientname = driver.find_element_by_css_selector(Clientbox)
    Clientname.send_keys('Southern')

def t18():
    Clientbox = 'body > table:nth-child(6) > tbody > tr > td:nth-child(2) > form 
    > table > tbody > tr:nth-child(1) > td:nth-child(1) > table > tbody > 
    tr:nth-child(7) > td:nth-child(3) > table > tbody > tr:nth-child(1) > 
     td:nth-child(2) > select'
    Clientname = driver.find_element_by_css_selector(Clientbox)
    Clientname.send_keys('Star')



t3() Thenewway() InspectTickets() newticket() t4() Thenewway() InspectTickets() newticket() t5() Thenewway() I

nspectTickets()

有没有更好的办法?可能有一个循环。新来的pyhton。谢谢你的建议!你知道吗


Tags: formchilddefdrivertablebodyfindselect
2条回答

显而易见的解决方案是不要定义一堆函数,然后试着用名字来调用它们。这和变量名的问题是一样的:它们会导致无法维护的代码。几乎总有更好的总体方法可以让您使用自然语言构造(枚举变量或函数不是自然的)。你知道吗

如果您坚持要拥有大量函数(但应重构代码以避免这种情况),则可以将所有函数附加到一个列表中,并分别调用它们:

all_funs = []

def foo(args):
    return 'hello'
all_funs.append(foo)

# ...
for fun in all_funs:
    # call the function in a loop
    print(fun())

或者我可以想象一个装饰器为您将函数收集到一个容器中。你知道吗

无论哪种方法,您都必须做一些额外的工作,因为您正试图在python中做一些强有力的工作。这种语言的好处是,简单的事情很容易做,所以如果你看到自己做了很多看似不必要的工作,那么你可能是在做不必要的工作。后退几步,尝试重写代码,使其更自然。例如,t17t18只在传递给.send_keys的单个字符串中不同。您应该将其作为输入参数并使用相同的函数。我确信你的20个函数中的大多数都可以用同样的方法简化,使用一个或两个底层函数。你知道吗

安德拉斯·迪克的答案是完美的。 我写下我的答案只是希望能更好地理解你的问题。 看看你的函数,似乎你需要,对于一个元素

driver.find_element_by_css_selector

将字符串发送到文本框中

如果您只需要将不同的字符串发送到同一个文本框中(在我看来,您指向所附的两个示例fun中的同一个元素),那么您不需要使用许多函数,正如Andras所说的那样。你知道吗

只需使用for循环:

def t17():
    Clientbox = 'body > table:nth-child(6) > tbody > tr > td:nth-child(2) > form 
> table > tbody > tr:nth-child(1) > td:nth-child(1) > table > tbody > 
tr:nth-child(7) > td:nth-child(3) > table > tbody > tr:nth-child(1) > 
td:nth-child(2) > select'
    Clientname = driver.find_element_by_css_selector(Clientbox)
    # example list:
    key_list = ["Southern", "Star"]
    for key in key_list:
        Clientname.send_keys(key)

当然,如果您需要,请不要忘记同时发送ENTER键:

# this at top of the file:
from selenium.webdriver.common.keys import Keys
# in yout function:
Clientname.send_keys(Keys.ENTER))

如果您需要等待页面重新加载,那么也可以使用Selenium库中的wait方法:

from selenium.webdriver.support import wait as Wait
from selenium.common.exceptions import TimeoutException

try:
    Wait(driver, 10).until(lambda driver: driver.find_element_by_css_selector(Clientbox).is_displayed())
except TimeoutException as e:
    print("here handle your exception", e)

这将等待最多10秒(您当然可以自定义它!)然后它会抛出一个TimeoutException,表明您想要的元素(甚至整个页面)尚未加载

我希望我能理解你的问题,这会有所帮助。你知道吗


如果您确实需要通过数字(或选择名称)调用函数,那么您可以尝试使用字典,它提供了比列表更多的控制,如果您不需要每次都循环整个列出的项:

def d1():
    return "it's d1"


def d2():
    return "it's d2"


def d3():
    return "it's d3"


def d4():
    return "it's d4"


def fun_switcher(num):
    switcher = {1: d1, 2: d2, 3: d3}
    return switcher.get(num, lambda: "fun address error")

# execution example:
if __name__ == "__main__":

    num = 2
    result = fun_switcher(num)
   # remember to call the function, or you will get the function_object as result!
    print(result())

相关问题 更多 >

    热门问题