【发布时间】:2021-07-27 10:21:01
【问题描述】:
我是测试自动化领域的新手,并且一直在自学。
在互联网挑战 DOM 页面上,我使用绝对 Xpath 找到了蓝色按钮,这是我唯一能想到的找到它。随着页面的刷新,其他所有选择器似乎都在动态变化。
在这种情况下,有没有办法让这个选择更加稳健?从谷歌搜索来看,这似乎是一种在网页上定位内容的脆弱方法。
https://the-internet.herokuapp.com/challenging_dom
''' #Test blue button on page, repeated 5 times and text displayed checked against options.
@pytest.mark.repeat(5)
def test_2():
elem1 = b.find_element(By.XPATH, 'html/body/div[2]/div/div/div/div/div[1]/a[1]')
a=(elem1.text)`enter code here`
elem1.click()
assert a in Names
print (a)'''
【问题讨论】:
-
请试试这个并告诉我 xPath = //a[@class='button' and text()='foo']
-
这应该适用于您的情况
//a[contains(@class, 'button') and position() = 1]。另外position() = 1会选择第一个按钮,你可以根据你要点击的来改变值。
标签: python selenium testing pytest