简体   繁体   English

selenium webdriver(c#)-循环访问选择菜单的最佳方法-然后验证元素样式是否更新?

[英]selenium webdriver (c#) - Best way to iterate through a select menu - Then verify an elements style updates?

I would like to iterate through all available options inside a select menu, click on each option in turn, then verify that clicking on each option will update a particular element with a width and height value. 我想遍历选择菜单中的所有可用选项,依次单击每个选项,然后确认单击每个选项将用宽度和高度值更新特定的元素。

Here is the code for the select menu: 这是选择菜单的代码:

 <span id="SizeSelectContainer" class="dropdownButton" style="min-width: 100px; max-width: 120px;">
            <select id="SizeSelect" title="Zoom" tabindex="23" data-l10n-id="zoom" style="min-width: 126px;">
                <option id="SizeAutoOption" title="" value="auto" selected="selected" data-l10n-id="pagesizeauto">Automatic Size</option>
                <option id="SizeActualOption" title="" value="page-size-actual" data-l10n-id="pagesizeactual">Actual Size</option>
                <option title="" value="0.1">10</option>
                <option title="" value="0.5">50</option>
                <option title="" value="1">100</option>                         
            </select>
        </span>

When I click on each option, the value within another element located elsewhere on the page will change (see below) - There is no "selected" attribute applied to any of the drop down options when selected (apart from the default option) so I can't assert the selections based on a "selected" attribute. 当我单击每个选项时,位于页面其他位置的另一个元素内的值将更改(请参见下文)-选中后,除下拉选项(默认选项)外,没有任何“选择”属性应用于任何下拉选项无法基于“选定”属性断言选择。

What I'm going to do (which I haven't coded in my sample yet) is that when a particular select option is selected, another page element (shown below) will update to hold new width and height values: 我要做的事情(我尚未在示例中进行编码)是,当选择了特定的选择选项时,另一个页面元素(如下所示)将更新以容纳新的宽度和高度值:

<div id="Container" class="page" style="width: 547px; height: 865px; data-loaded="true">

Here is what I have come up with so far - Note that I know I'm not asserting anything yet 这是我到目前为止提出的-请注意,我还没有断言

        IWebElement AutoSize = UtilityClass.driver.FindElement(By.Id("SizeAutoOption"));
        IWebElement ActualSize = UtilityClass.driver.FindElement(By.Id("SizeActualOption"));

        IWebElement Ten = UtilityClass.driver.FindElement(By.XPath("//*[@id='SizeSelect']/option[3]"));
        IWebElement Fifty = UtilityClass.driver.FindElement(By.XPath("//*[@id='SizeSelect']/option[4]"));
        IWebElement Hundred = UtilityClass.driver.FindElement(By.XPath("//*[@id='SizeSelect']/option[5]"));  

        Object[] SelectElements = new Object[] { AutoSize, ActualSize, Ten, Fifty, Hundred };

        for (int i = 0; i < SelectElements.Count(); i++)
        {
            if (i == 0)
            {
                AutoSize.Click();
                System.Threading.Thread.Sleep(1000);
            }
            else if (i == 1)
            {
                ActualSize.Click();
                System.Threading.Thread.Sleep(1000);
            }
            else if (i == 2)
            {
                Ten.Click();
                System.Threading.Thread.Sleep(1000);
            }
            else if (i == 3)
            {
                Fifty.Click();
                System.Threading.Thread.Sleep(1000);
            }
            else if (i == 4)
            {
                Hundred.Click();
                System.Threading.Thread.Sleep(1000);
            }
         }

I'm really just looking for some confirmation on whether or not this will be the best way to approach this. 我真的只是在寻找一些确认,以确认这是否是实现此目标的最佳方法。 The reason I ask is that I have essentially simplified the code sample above and I actually have over 50 select options to verify - not to mention that I have many other select menus with as many options contained inside them to automate. 我问的原因是我实质上简化了上面的代码示例,实际上我有50多个选择选项可进行验证-更不用说我还有许多其他选择菜单,其中包含的许多选项可以自动化。

You've got several problems here. 您在这里遇到了几个问题。

The first is that a for loop is not what you want to do. 首先是for循环不是您想要执行的操作。

The second is that Selenium wraps all the "find me an option within a select" functionality for you in a SelectElement class so you don't have to do this in the first place: 第二个是Selenium在SelectElement类中为您包装了所有“在select中给我一个选项”功能,因此您不必首先这样做:

The third is that your XPath constructs are heavily based on position. 第三是您的XPath构造很大程度上取决于位置。 Position is bad because it's something that can change. 位置不好,因为它可以改变。 If I add in an option at the very top of that list, your test will break and it should not. 如果我在该列表的顶部添加了一个选项,则您的测试将失败,并且应该不会。 I've not broken application functionality, merely just changed the list order, and your test is going to fail. 我没有破坏应用程序的功能,只是改变了列表顺序,而您的测试将失败。

The fourth problem is you are holding references to your element and you've said the page is going to change when you click the option. 第四个问题是您持有对元素的引用,并且您说单击选项时页面将发生变化。 This, may , cause a StaleReferenceException dependant on what the click actually does. 可能会导致StaleReferenceException具体取决于单击的实际操作。 I say may because it shouldn't but if your application is coded to do something that causes Selenium to think "the reference to this is no longer valid" it will throw exceptions. 我说这可能是因为它不应该,但是如果您的应用程序被编码为导致Selenium认为“对此的引用不再有效”,它将引发异常。

The fifth issue is you are looping through an array of object s which is unnecessary. 第五个问题是您正在遍历不必要的object数组。 You know their type. 你知道他们的类型。 It's IWebElement . 这是IWebElement You are not harnessing the power of the class if you just define it as Object . 如果仅将其定义为Object则无法利用该类的功能。

The final issue is that you are using Thread.Sleep . 最后一个问题是您正在使用Thread.Sleep You are clearly waiting for something so use the explicit wait within Selenium to actually wait for something instead of "lets wait a few seconds". 您显然正在等待某事,因此请使用Selenium中的显式等待来实际等待某事,而不是“让我们等待几秒钟”。

https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/UI/SelectElement.cs https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/UI/SelectElement.cs

(remember Selenium is open source) (请记住,Selenium是开源的)

SelectElement actualSizeSelect = new SelectElement(UtilityClass.driver.FindElement(By.Id("SizeSelect")));

This then gives you the ability to search by text: 然后,您便可以按文本进行搜索:

actualSizeSelect.SelectByText("10");

Now your test won't care where exactly in that list "10" is! 现在,您的测试将不在乎列表“ 10”中的确切位置!

This would also give you the ability to loop through all the options within a given select like so: 这也使您能够遍历给定select所有选项,如下所示:

IList<IWebElement> options = actualSizeSelect.AllOptions;

You now have a list of option s within that select . 现在,在select有一个option列表。 So let's loop through them: 因此,让我们遍历它们:

foreach (IWebElement option in options) 
{
    option.Click(); // just a demo to show you.
}

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

相关问题 c# - 遍历数据中继器中所有行的最佳方法? - c# - best way to iterate through all rows in a datarepeater? 有没有办法(在 c# 中)遍历网站 HTML 上的文章元素? - Is there a way (in c#) to iterate through article elements on a website's HTML? 使用 Selenium WebDriver(C#、Windows 表单)遍历多个图像 - Iterate through multiple Images using the Selenium WebDriver (C#, Windows Forms) 具有相似元素的Selenium Webdriver C#页面 - Selenium Webdriver C# Pages with Similar Elements 识别警报硒WebDriver C#上的元素 - Identifying Elements on Alert selenium webdriver c# Q:遍历表元素并通过 CSS styles 进行识别(c# 中的 selenium webdriver) - Q: Looping through table elements and identifying through CSS styles (selenium webdriver in c#) 在Selenium Webdriver [C#]中选择单选按钮 - Select Radio Buttons in Selenium Webdriver [C#] 使用C#在Selenium WebDriver中断言,验证和其他命令 - Assert, Verify and other commands in Selenium WebDriver using C# 如何在c#webdriver selenium test中用空格验证两个int - How to verify two int with spaces in c# webdriver selenium test 如何选择<span>使用Selenium WebDriver和C#</span>在<span>标签</span>下定义的下拉菜单项 - How to select dropdown menu item which is defined under <span> tag using Selenium WebDriver and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM