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。谢谢你的建议!你知道吗
显而易见的解决方案是不要定义一堆函数,然后试着用名字来调用它们。这和变量名的问题是一样的:它们会导致无法维护的代码。几乎总有更好的总体方法可以让您使用自然语言构造(枚举变量或函数不是自然的)。你知道吗
如果您坚持要拥有大量函数(但应重构代码以避免这种情况),则可以将所有函数附加到一个列表中,并分别调用它们:
或者我可以想象一个装饰器为您将函数收集到一个容器中。你知道吗
无论哪种方法,您都必须做一些额外的工作,因为您正试图在python中做一些强有力的工作。这种语言的好处是,简单的事情很容易做,所以如果你看到自己做了很多看似不必要的工作,那么你可能是在做不必要的工作。后退几步,尝试重写代码,使其更自然。例如,
t17
和t18
只在传递给.send_keys
的单个字符串中不同。您应该将其作为输入参数并使用相同的函数。我确信你的20个函数中的大多数都可以用同样的方法简化,使用一个或两个底层函数。你知道吗安德拉斯·迪克的答案是完美的。 我写下我的答案只是希望能更好地理解你的问题。 看看你的函数,似乎你需要,对于一个元素
将字符串发送到文本框中
如果您只需要将不同的字符串发送到同一个文本框中(在我看来,您指向所附的两个示例fun中的同一个元素),那么您不需要使用许多函数,正如Andras所说的那样。你知道吗
只需使用for循环:
当然,如果您需要,请不要忘记同时发送ENTER键:
如果您需要等待页面重新加载,那么也可以使用Selenium库中的wait方法:
这将等待最多10秒(您当然可以自定义它!)然后它会抛出一个TimeoutException,表明您想要的元素(甚至整个页面)尚未加载
我希望我能理解你的问题,这会有所帮助。你知道吗
如果您确实需要通过数字(或选择名称)调用函数,那么您可以尝试使用字典,它提供了比列表更多的控制,如果您不需要每次都循环整个列出的项:
相关问题 更多 >
编程相关推荐