硒-单击锚点后获取位置 [英] Selenium - Get location after clicking on an anchor

查看:111
本文介绍了硒-单击锚点后获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查以下事实:单击锚链接时,某个部分确实位于浏览器的顶部(希望在这里有意义).

我的测试如下:

class Tests(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(chromedriver)
        self.accept_next_alert = True
        self.driver.set_window_size(1100, 800)
        self.base_url = "http://someurl"

    def test_anchor(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.implicitly_wait(1)
        location = driver.find_element_by_xpath(
            "//section[@id='123123']").location
        print location

        driver.find_element_by_partial_link_text("anchor_text").click()
        location = driver.find_element_by_xpath(
            "//section[@id='123123']").location
        print location

        self.assertTrue(False)

断言只是为了让我可以看到我的照片. 我从中得到的是,即使我看到锚点起作用了,位置指令也保持不变.所以我想我没有使用正确的功能.

您能想到另一种方法来检查点击锚是否达到预期效果吗? (通过不查看位置或以正确的方式查看位置)

解决方案

另一种方法是,通过将屏幕快照与帮助进行比较,单击链接并断言页面(或元素,页面的一部分)看起来应该像它一样 cssneedle 包的内容:

from needle.cases import NeedleTestCase

class Tests(NeedleTestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(chromedriver)
        self.accept_next_alert = True
        self.driver.set_window_size(1100, 800)
        self.base_url = "http://someurl"

    def test_anchor(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.implicitly_wait(1)

        driver.find_element_by_partial_link_text("anchor_text").click()

        self.assertScreenshot('#my_element_id', 'screenshot-file-name')

要创建screenshot-file-name,您需要首先使用--with-save-baseline标志运行相同的测试:

nosetests my_test.py --with-save-baseline

I'm trying to check the fact that when I click on an anchor link, some section is really at the top of the browser (I hope I'm making sense here.)

My test looks like that:

class Tests(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(chromedriver)
        self.accept_next_alert = True
        self.driver.set_window_size(1100, 800)
        self.base_url = "http://someurl"

    def test_anchor(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.implicitly_wait(1)
        location = driver.find_element_by_xpath(
            "//section[@id='123123']").location
        print location

        driver.find_element_by_partial_link_text("anchor_text").click()
        location = driver.find_element_by_xpath(
            "//section[@id='123123']").location
        print location

        self.assertTrue(False)

The assertion is just so I can see my prints. What I get from that is that even though I see that the anchor worked, the location dict stays the same. So I guess I'm not using the right feature.

Could you think of another way to check that the click on the anchor had the expected effect? (by either not looking at the location or by looking at the location the right way or else)

解决方案

Another approach would be to click the link and assert that the page (or element, part of a page) looks like it should by comparing the screenshots with the help of cssneedle package:

from needle.cases import NeedleTestCase

class Tests(NeedleTestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(chromedriver)
        self.accept_next_alert = True
        self.driver.set_window_size(1100, 800)
        self.base_url = "http://someurl"

    def test_anchor(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.implicitly_wait(1)

        driver.find_element_by_partial_link_text("anchor_text").click()

        self.assertScreenshot('#my_element_id', 'screenshot-file-name')

To create the screenshot-file-name you need to first run the same tests with --with-save-baseline flag:

nosetests my_test.py --with-save-baseline

这篇关于硒-单击锚点后获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆