在两个不同的Python文件中使用参数

2024-05-19 02:09:43 发布

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

我试图在两个不同的python文件之间使用一个参数。我有locators.py,在这里我定义了定位器:

定位器.py

from selenium.webdriver.common.by import By

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

在这个文件中,我定义了i。你知道吗

主.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未定义。我正在导入定位器,但不知道为什么它不工作。你知道吗


Tags: frompyimportselfexecutebyisscript
1条回答
网友
1楼 · 发布于 2024-05-19 02:09:43

在一个类中,你不需要有你想要的值。你可以有这样一个文件:

主页locatars.py

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

然后在你的main.py

from MainPageLocatars import *

print param1

相关问题 更多 >

    热门问题