【selenium】,* 傳遞參數的問題: __init__() takes 2 positional arguments but 3 were given


一,在做自動化測試時候,顯示等待封裝一下定位元素的方法:

    def find_element(self, *loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(*loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            raise e 

 

二、腳本執行提示:

 

 

 意思就是,傳入了三個參數,但是只有兩個位置;

*loc 代表的是有三個參數:self,xpath和路徑地址

    login_password_loc = (By.XPATH, "//input[@type='password']")

*parameter是用來接受任意多個參數並將其放在一個元組中;

所以,函數在調用多個參數時,在列表、元組、集合、字典及其他可迭代對象作為實參,並在前面加 *

如   *(1,2,3)解釋器將自動進行解包然后傳遞給多個單變量參數(參數個數要對應相等)

修改為:

   def find_element(self, *loc):
        try:
            WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located(loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            raise e

  解決問題。

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM