2016-12-08 27 views
0

我想在兩個不同的python文件之間使用參數。我有locators.py,在那裏我已經定義了定位:在兩個不同的Python文件中使用參數

locators.py

from selenium.webdriver.common.by import By 

    class MainPageLocatars(object): 
     ELEMENT = (By.XPATH, "//label[text() = '%s']"%(i)) 

在這個文件中,我有i定義。

main.py

import locators 
from locators import * 
from locators import MainPageLocatars 

def __selectCheckbox(self, checkbox, locater): 
     if checkbox == "All": 
      if self.driver.find_element_by_id(locater).is_selected() == False: 
       self.execute_script_click(MainPageLocatars.CHECKBOX) 
     else: 
      if self.driver.find_element_by_id(locater).is_selected() == False: 
       self.execute_script_click(MainPageLocatars.CHECKBOX) 
       self.execute_script_click(MainPageLocatars.CHECKBOX) 
      elif self.driver.find_element_by_id(locater).is_selected() == True: 
       self.execute_script_click(MainPageLocatars.CHECKBOX) 
      for i in checkbox: 
       # only this element is not defined in locators.py 
       self.execute_script_click(*MainPageLocatars.ELEMENT) 

當我運行這段代碼,我得到一個錯誤說沒有定義i。我正在導入定位器,但不知道爲什麼它不起作用。

回答

0

你不需要有你在班級內的價值。你只需有一個這樣的文件:

MainPageLocatars.py

param1 = "locator1" 
param2 = "locator2" 
. . . 

,然後在main.py

from MainPageLocatars import * 

print param1 
+0

我這裏的問題是,在main.py我我在循環中使用變量i。然後在locatars.py中,我使用的是「我」,當我運行測試時,它說我沒有定義 – user7242550

+0

@ user7242550您是否嘗試設置locators.py文件? –

+0

是的,但它不工作,因爲在我的情況下,param1不是一個簡單的文本。它是我的main.py中定義的定位器 – user7242550

相關問題