我有一个使用seleniumide捕获的ID列表,并将它们放到PyCharm中。列表如下所示:
filter_logs_query = (By.ID, "Query")
filter_logs_FromDateUI = (By.ID, "FromDateUI")
filter_logs_ToDateUI = (By.ID, "ToDateUI")
filter_logs_Ip = (By.ID, "Ip")
filter_logs_User = (By.ID, "User")
filter_logs_Message = (By.ID, "Message")
filter_logs_RefID = (By.ID, "Refid")
filter_logs_Session = (By.ID, "Session")
filter_logs_Scope = (By.ID, "Scope")
filter_logs_EventNo = (By.ID, "EventNo")
filter_logs_Server = (By.ID, "Server")
每个ID都有一个def供用户向ID发送密钥,列表如下所示:
def filter_logs_search_query(self, word):
search_text = self.driver.find_element(*Elements.filter_logs_query)
search_text.send_keys(word)
def filter_logs_logged_after(self, word):
search_date = self.driver.find_element(*Elements.filter_logs_FromDateUI)
search_date.send_keys(word)
def filter_logs_logged_before(self, word):
search_date = self.driver.find_element(*Elements.filter_logs_ToDateUI)
search_date.send_keys(word)
除了需要从*元素调用的函数外,我的def几乎是相同的。我有没有办法压缩/缩短我的def列表?你知道吗
这是Higher Order Functions.的工作
每当您遇到代码相同的情况时,除了要调用的函数,最好只传入该函数(注意
f
):然后,其他函数可以用广义高阶函数来定义。例如:
注意,你的重复函数不是很大,所以这里的增益不是很大。不过,这种技术在处理更复杂的函数时特别有用。泛化一个函数的好处是,如果需要对它进行更改,只需更改一个函数(常规函数),而不是所有的“克隆”函数。你知道吗
还要注意,这似乎是
find_element
使用的技术!它不强迫您编写自己的自定义find方法,而是允许您只传入函数。你现在需要做的只是推回谁负责选择传递什么函数的责任。你知道吗是的。您已经在发送密钥类型为的字符串。使用if语句或制作使用字符串作为键的字典。你知道吗
在构造函数中定义字典:
然后使用字典“选择”元素:
看起来整件事都可以压缩到
相关问题 更多 >
编程相关推荐