【发布时间】:2020-09-01 14:03:59
【问题描述】:
我正在运行一个 django 应用程序,在该范围内我有一个带有导航栏的页面,当我点击我的 contact-tab 时,它会自动向下滚动到我的联系人部分。
我正在尝试使用 selenium 测试这种行为,但我似乎无法弄清楚如何测试页面的实际位置。或者,换句话说,我想验证页面是否真的向下滚动到了联系人部分。
现在我正在这样做:
def test_verify_position(
self, browser, live_server
):
""""""
browser.get(live_server.url)
contact_section = browser.find_element_by_id("contact").click()
assert ????
我想我必须以某种方式获取当前滚动位置坐标。我知道我可以使用.location 获取元素的位置。但无论滚动位置如何,元素的相对位置始终相同。我试过这个来调试:
def test_verify_position(
self, browser, live_server
):
""""""
browser.get(live_server.url)
e = browser.find_element_by_xpath(
"/html/body/section[4]/div/div[1]/h2/strong")
location = e.location
print(location)
browser.find_element_by_css_selector(".nav-contact").click()
e = browser.find_element_by_xpath("/html/body/section[4]/div/div[1]/h2/strong")
location = e.location
print(location)
这会打印滚动前后的相同坐标。
我还搜索了官方文档https://www.selenium.dev/documentation/en/webdriver/web_element/,但找不到更好的解决方案或任何解决方案。
有人知道怎么做吗?非常感谢您的帮助。提前致谢!
【问题讨论】:
标签: python django selenium pytest-django