Selenium Webdriver错误否定的结果,用于测试元素的可见性? [英] Selenium Webdriver false negative result for testing visibility of an element?

查看:159
本文介绍了Selenium Webdriver错误否定的结果,用于测试元素的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Selenium Webdriver有问题.我正在针对Web应用程序运行自动化测试,并且有一个测试来检查元素的可见性.当我在CLI中运行同一行脚本时,会找到该元素,并且is_displayed()返回"TRUE",但是当我通过Python IDE与其他测试用例一起运行时,is_displayed()返回False.

I have an issue with Selenium Webdriver. I am running my automated test against a web application and have a test to check visibility of an element. When I run the same line of script in CLI, the element is found and is_displayed() returns "TRUE" but When I run it with other test cases through my Python IDE, is_displayed() returns False.

我尝试了隐式和显式等待,但是等待不是问题,并且元素已经加载.

I tried implicit and explicit waits but wait is not the issue and the element is already loaded.

这是我的测试脚本中的测试用例:

Here is the test case in my test script:

def test_image_element_icon_is_displayed(self):
    self.assertTrue(self.driver.find_element_by_css_selector("#Image").is_displayed(),msg="Image is not displayed")

这是我在CLI中运行的脚本:

And this is script I run in CLI:

 driver.find_element_by_css_selector("#Image").is_displayed()

有什么办法可以防止这种假阴性?也许更改我当前正在使用的元素选择器?

Is there any way that I can prevent this false negative? Maybe changing the element selector I am currently using?

这是我的Image元素的HTML:

Here is the HTML of my Image element:

<div ng-click="applyDataElement(toolbarButton.type, toolbarButton.id)" class="frame-click ng-scope" ng-repeat="toolbarButton in toolbar">
                <div class="frame-box" ng-attr-id="{{toolbarButton.id}}" id="Image">
                    <div class="draggable-box">
                        <div bind-html-unsafe="toolbarButton.icon" class="draggable-icon ng-binding"><i class="fa fa-picture-o"></i></div>
                        <div class="draggable-label ng-binding">Image</div>
                    </div>
                </div>
            </div>

推荐答案

我很确定此ID不能唯一,因为它位于ng-repeat中,并且可能加载了多个具有相同ID的元素,而不是使用安全.

I am pretty sure this id cannot be unique since it is in ng-repeat and it's probably loading more than one elements with same id which is not safe to use.

话虽如此,并且既然您还提到了元素加载不是问题,所以可以安全地假设选择器可能返回实际上被隐藏的其他元素.在硒实际上发现了隐藏的元素而不是预期的元素之前,我经历了类似的事情.实际上,您可以将len()find_elements_by_css_selector()结合使用,以查看返回了多少个元素.

With that being said and since, you also mentioned element load is not an issue, it safe to assume that the selector is probably returning some other element which is actually hidden. I experienced something similar before where selenium actually found the element which was hidden and was not the intended one. You can actually use len() with find_elements_by_css_selector() to see how many elements were returned.

ng-attr-id="{{toolbarButton.id}}"或客户端上显示的任何内容与[id='image'][ng-attr-id='something']一起用作cssSelector

Use ng-attr-id="{{toolbarButton.id}}" or whatever displayed on client in cojuntion with [id='image'][ng-attr-id='something'] as cssSelector

这篇关于Selenium Webdriver错误否定的结果,用于测试元素的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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