使用Enum为selenium tes构建定位器

2024-09-28 01:23:55 发布

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

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

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。在

^{pr2}$

对我来说,列出所有对应元素及其定位器的最佳方法是什么。在


Tags: pydivweb元素selenium选择器enumcss
1条回答
网友
1楼 · 发布于 2024-09-28 01:23:55

你可以用

在位于的元素的EC可见性定位元素

http://selenium-python.readthedocs.io/waits.html

样本代码:

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

你也可以

^{pr2}$

这可以传递元组来定位元素吗

相关问题 更多 >

    热门问题