作者jacobcan118 (jacobcan118)
看板Python
标题[问题] Enum建立大量的字串
时间Thu May 4 10:18:33 2017
我现在用python去读大量的对应变数来跑selenium页面测试.每个页面的element都有对应的字串, 我想用python
的Enum去建一个档大概像下面一样.
file: ElementEnum.py
from enum import Enum
class PageA(Enum):
pageAelementA = '//div[{}]/a'
pageAelementB = '//button[.="{}"]'
pageAelementC = '//tr[{}]/td[{}]'
class PageC(Enum):
pageBelementA = '//div[{}]/a'
pageBelementB = '//button[.="{}"]'
pageBelementC = '//tr[{}]/td[{}]'
class PageB(Enum):
pageCelementA = '//div[{}]/a'
pageCelementB = '//button[.="{}"]'
pageCelementC = '//tr[{}]/td[{}]'
但之後在用後, 发现常常需要用format去建完整的字串变成可读的xpath或是css selector字串.想请问大家有没有比较好的建议, 因为当我在建locator有很多需要format的地方会看起来很杂乱
import ElementEnum.PageA as PA
driver.find_element_by_xpath('{}//table/thead[{}]'.format(PA.pageAelementA.value, 1))
func(index, **kwargs):
driver.find_element_by_xpath('//table/thead[{}]/tr[{}]/td[{}]/{}'.format(index, kwargs['row'], kwargs['col'], PA.pageBelementA.value))
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 108.14.0.213
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1493864315.A.4A6.html
1F:推 ides13: 将所有的format编写成字典,然後用key调用?Dpath[key]. 05/04 11:38
2F:→ jacobcan118: 不过我很多需要format的时候都是变数在跑降可以编成 05/04 19:40
3F:→ jacobcan118: 字典吗 05/04 19:40
4F:推 ides13: 我不确定,但将format编成字典是cookbook中的例子内的作法 05/04 21:48
6F:→ jacobcan118: 我已经在字符串中插入变量了. 但是还是会有很多情形 05/05 11:38
7F:→ jacobcan118: 像是我想要找在pageA里elemetnA下的elementB会变成 05/05 11:39
8F:→ jacobcan118: locator = '{}/{}'.format(PageA.pageAelementA.valu 05/05 11:40
9F:→ jacobcan118: e.format(xxx), PageA.pageAelementB.value.format(x 05/05 11:41
10F:→ zerof: 如果都是用 Enum.value 的话改用 namedtuple 会比较好 05/05 11:53
12F:→ jacobcan118: 感谢 学了一下namedtuple 如果我每一个class 只给值 05/11 07:28
13F:→ jacobcan118: 一次 有人降用吗 05/11 07:28
14F:→ zerof: 跟你用 Enum 的概念是一样的吧...... 05/11 10:55
16F:→ ides13: Avoid overengineering datastructures. 05/11 18:04
17F:→ ides13: Tuples are better than objects (try namdtuple too 05/11 18:05
18F:→ ides13: too though).Prefer simple fields over getter/setter fn 05/11 18:06