简体   繁体   English

如何仅计算显示在WebPage上的元素。 硒

[英]How to count only elements displayed on WebPage. Selenium

I have a web page with two tabs. 我有一个带有两个标签的网页。

When I get count of Elements on one tab it gives me count for both the tabs but not on the one I have clicked on. 当我在一个选项卡上获得元素计数时,它将为我同时显示两个选项卡的计数,而不是我单击的那个计数。

So If I want to check an element that is present of Tab1 and not on Tab2 I am not able to do that as I am getting same results on both the tabs. 因此,如果我要检查Tab1上存在的元素,而不是Tab2上没有的元素,则无法执行此操作,因为在两个选项卡上都得到相同的结果。

if (driver.FindElements(By.XPath("//div[contains(text(), 'Dummy Text')]")).Count != 0)
{
    // Failed Test message ;
}

On Tab1 I am expecting count should be 0 as this text is not displayed and on Tab2 count should 8 but on both the tabs count is 8. 我希望在Tab1上计数为0,因为不会显示此文本,在Tab2上计数应该为8,但在两个选项卡上的计数均为8。

How can I get count of elements which are displayed on my page and not in the background? 如何获取显示在页面而非背景中的元素的数量?

I am sure it is because of tabs on web page and selenium is considering both tabs as same web page content. 我敢肯定这是因为网页上的标签页,硒将这两个标签页视为相同的网页内容。

Selenium will return all objects in the DOM not just those visible to the user. Selenium将返回DOM中的所有对象,而不仅仅是用户可见的那些对象。

I do not believe there is any XPath or CSS selector which allows you to select only visible elements. 我不相信有任何XPath或CSS选择器仅允许您选择可见元素。 I can think of a potential nasty XPath which ensure that no parent had display:none but do not know if that would actually work and sounds too horrible and complex to even contemplate. 我可以想到一个潜在的令人讨厌的XPath,它可以确保没有父级显示:无,但不知道这样是否真的有效,并且听起来太可怕,太复杂了,甚至无法考虑。

You need to iterate through each returned element and check that "displayed" returns false. 您需要遍历每个返回的元素,并检查“ displayed”是否返回false。 I would be tempted to extend the Driver class to have a FindDisplayedElements method which would wrap this functionality for you. 我很想将Driver类扩展为具有FindDisplayedElements方法,该方法将为您包装此功能。

However this approach could be expensive in terms of API calls if you are using RemoteWebDriver. 但是,如果您使用RemoteWebDriver,则此方法在API调用方面可能会很昂贵。 An alternative could be to ensure a parent object is hidden, as all children would also inherit that behaviour 另一种选择是确保父对象被隐藏,因为所有子对象也将继承该行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM