Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Enum为selenium测试构建定位器_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 使用Enum为selenium测试构建定位器

Python 使用Enum为selenium测试构建定位器,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我提出了一个关于selenium web测试的问题。因为所有元素都要求自己的xpath或css选择器由SeleniumWebDriver执行操作。 我尝试过使用PythonEnum并创建类似 file: elementEnum.py from enum import Enum class PageA(Enum): pElementA = '//div[{}]' pElementB = '//a[.="{}"]' pElementC = '//div[@class={}]

我提出了一个关于selenium web测试的问题。因为所有元素都要求自己的xpath或css选择器由SeleniumWebDriver执行操作。 我尝试过使用PythonEnum并创建类似

file: elementEnum.py
from enum import Enum

class PageA(Enum):
    pElementA = '//div[{}]'
    pElementB = '//a[.="{}"]'
    pElementC = '//div[@class={}]'

class PageB(Enum):
    pElementA = '//button[.="{}""]'
    pElementB = '//table/tr[{}]/td[{}]'
但事实证明,我需要很多时间来构建python格式的字符串函数,而它并没有看到pythonoic

from elementEnum import *
driver.find_element_by_xpath('//div[@class="aaaaaa"]/{}'.format((PageA.pElementA.value).format(1)))
driver.find_element_by_xpath('{}/{}'.format(PageA.pElementA.value).format(1), PageA.pElementB.value.format(2)))
driver.find_element_by_xpath('{}/{}'.format(PageB.pElementB.value).format(1, 3), PageA.pElementA.value.format(2)))
列出所有对应元素及其定位器的最佳方法是什么。

您可以使用

EC.U元素的可见性\U定位元素

示例代码:

class SeleniumBaseClass(object):
    def __init__(self,driver):
        self.driver = driver
    def open(self,URL):
        self.driver.get(URL)
    def driverURLChange(self,URL):  
        print("change URL" + URL)
        self.driver.get(URL)
    def currentUrl(self):
        print("URL   " +  self.driver.current_url)
        return self.driver.current_url
    def switchNewWindow(self):
        self.driver.switch_to_window(self.driver.window_handles[1])
        return self.driver.title
    def locateElement(self, loc):
        try:
            print(loc)
            element = WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
            return element
        except:
            print ("cannot find {0} element".format(loc))
        return None
你可以

password_loc =(By.NAME,'password')
webdriver = SeleniumBaseClass(driver)
webdriver.locateElement(password_loc )
这可以通过传递元组来定位元素