简体   繁体   English

如何在列表框中使用Selenium选择一个下拉项目,并在每个页面加载时使用动态(更改)Xpath值

[英]How to select a drop down item using Selenium in C# from a Listbox with Dynamic (changing) Xpath value on every page load

For my automated test using selenium in C#, I want to select a drop down list item (any) from the type field (ref attached image), however since the id's/xpath for selected element getting generated dynamically at every page load, my selected element click is failing when I run my recorded script. 对于我在C#中使用selenium进行自动化测试,我想从类型字段(ref附加图像)中选择一个下拉列表项(any),但是由于所选元素的id / xpath是在每次加载页面时动态生成的,所以我选择了运行录制的脚本时,元素单击失败。

在此输入图像描述 For example my below script fail, as XPath "//select[@name='Entries[ db9ef219-0f54-4925-9589-0f39351f44a4 ].TypeID']" changes every time as I run test in VS.Net. 例如,我的下面脚本失败,因为XPath“//选择[@ name ='条目[ db9ef219-0f54-4925-9589-0f39351f44a4 ] .TypeID']”每次在VS.Net中运行测试时都会更改。 Value db9ef219-0f54-4925-9589-0f39351f44a4 changes every time when page loads. 每次页面加载时,值db9ef219-0f54-4925-9589-0f39351f44a4都会更改。

IWebElement selectType =
            driver.FindElement(By.XPath("//select[@name='Entries[db9ef219-0f54-4925-9589-0f39351f44a4].TypeID']"));
        selectType.Click();

Here is the page code - on load for each new row a unique value (below it's c36582c1-131a-4f6f-8711-390048f5779f ) is generated and stored under class RegEffEntryContainer , and is used for each list elements - type/description ( eg id="Entries_c36582c1-131a-4f6f-8711-390048f5779f__TypeID", id= "Entries_c36582c1-131a-4f6f-8711-390048f5779f__Organisation") 这是页面代码 - 在每个新行的加载时生成一个唯一值(低于它的c36582c1-131a-4f6f-8711-390048f5779f )并存储在类RegEffEntryContainer下 ,并用于每个列表元素 - 类型/描述(例如id =“Entries_c36582c1-131a-4f6f-8711-390048f5779f__TypeID”,id =“Entries_c36582c1-131a-4f6f-8711-390048f5779f__Organisation”)

在此输入图像描述

Any help in resolving this would be highly appreciated - thanks in advance! 任何帮助解决这个问题将非常感谢 - 提前感谢!

FYI : If I use the below code using Dynamic XPath, it will only work for the first row and I would not be able to record any subsequent row entries via the script. 仅供参考 :如果我使用动态XPath使用下面的代码,它只适用于第一行,我将无法通过脚本记录任何后续行条目。 Requirement is to enter all 3 entrees before clicking Save/Submit button (not shown on the screenshot above). 要求是在单击“ 保存/提交”按钮之前输入所有3个主菜(上面的屏幕截图中未显示)。

IWebElement selectType = driver.FindElement(By.XPath("//select[contains(@id, '__TypeID')]"));

Thank you @JeffC for your suggestion - adding the html code as suggested - I've still kept the code image provided earlier. 感谢@JeffC的建议 - 按照建议添加html代码 - 我仍然保留了之前提供的代码图像。

<div class="RegEffEntryContainer" xpath="1">
<div class="row">
    <div class="col-11">
      <input type="hidden" name="Entries.index" autocomplete="off" value="21b28f6b-8aaa-4924-815a-1d925585fa36">
      <input data-val="true" data-val-number="The field EntryID must be a number." data-val-required="The EntryID field is required." id="Entries_21b28f6b-8aaa-4924-815a-1d925585fa36__EntryID" name="Entries[21b28f6b-8aaa-4924-815a-1d925585fa36].EntryID" type="hidden" value="391">                
            <div class="row">
                <div class="col-12 col-sm-6 col-lg-3">
                    <div class="field-wrapper">
                       <select class="form-control valid" data-val="true" data-val-number="The field TypeID must be a number." id="Entries_21b28f6b-8aaa-4924-815a-1d925585fa36__TypeID" name="Entries[21b28f6b-8aaa-4924-815a-1d925585fa36].TypeID" required="" aria-describedby="Entries_21b28f6b-8aaa-4924-815a-1d925585fa36__TypeID-error" aria-invalid="false">
                          <option value="">Select type</option>
                          <option value="1">Regulatory Activity</option>
                          <option value="2">Major Project</option>
                          <option value="3">Other Activities</option>
                       </select><span class="asterisk">*</span>
                    </div>
                </div>

Update[27/02/2019 7:00 PM] - since the numeric id's ( value: 21b28f6b-8aaa-4924-815a-1d925585fa36 above) used for TypeID/OrganisationID is dynamically created at page load, is there a way using Javascript to record this in a variable at page load and reuse that to create a XPath for element identification later? 更新[27/02/2019 7:00 PM] - 因为用于TypeID / OrganisationID的数字ID( 值:21b28f6b-8aaa-4924-815a-1d925585fa36 )是在页面加载时动态创建的,有没有办法使用Javascript来实现在页面加载时将其记录在变量中并重新使用它以便稍后创建用于元素标识的XPath? 在此输入图像描述

If you know the exact count of the row then simply use this 如果您知道行的确切数量,那么只需使用它

IWebElement firstSelect = driver.FindElement(By.XPath("//select[contains(@id, '__TypeID')][1]")); 
IWebElement secondSelect = driver.FindElement(By.XPath("//select[contains(@id, '__TypeID')][2]"));
IWebElement thirdSelect = driver.FindElement(By.XPath("//select[contains(@id, '__TypeID')][3]"));

This might not be a perfect answer, or the best way to do this, but I was able to solve this issue using JavaScript . 这可能不是一个完美的答案,或者是最好的方法,但我能够使用JavaScript解决这个问题。 Used an Array to store the dynamic ids at run time, and later used them to create string to find the required element on DOM. 使用Array在运行时存储动态ID,稍后使用它们创建字符串以在DOM上查找所需的元素。 Below is the my code. 以下是我的代码。

int numberOfEntriesOnPage = System.Convert.ToInt32(((IJavaScriptExecutor)driver).ExecuteScript("return document.getElementsByName('Entries.index').length"));
string[] array = new string[numberOfEntriesOnPage];
         for (int a = 0; a < numberOfEntriesOnPage; a++)
            {
                String script = "return document.getElementsByName('Entries.index')[" + a + "].value";
                array[a] = ((IJavaScriptExecutor)driver).ExecuteScript(script).ToString();
                Console.WriteLine("Array value:" + array[a]);
                string rowTypeID = "Entries_" + array[a] + "__TypeID";

                select_select_by_index(By.Id("Entries_" + array[a] + "__TypeID"), 1);
                IWebElement selectOrg = find_element(By.Name("Entries[" + array[a] + "].OrganisationID_input"));
                selectOrg.Clear();
                selectOrg.SendKeys("3801 LTD");

                IWebElement selectInOffice = driver.FindElement(By.Id("Entries_" + array[a] + "__InOffice"));
                selectInOffice.Clear();
                selectInOffice.SendKeys("10");

                IWebElement selectOffsite = driver.FindElement(By.Id("Entries_" + array[a] + "__Offsite"));
                selectOffsite.Clear();
                selectOffsite.SendKeys("5");

                IWebElement comments = driver.FindElement(By.Id("Entries_" + array[a] + "__Comment"));
                comments.Clear();
                comments.SendKeys(array[a] + "Manish test");

                IWebElement save = find_element(By.XPath("//button[@value='SaveDraft']"));

                save.Click();

            }

public static void select_select_by_index(By by, int index)
    {
        var select = new SelectElement(find_element(by));
        select.SelectByIndex(index);
    }

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

相关问题 如何使用Selenium Webdriver C#从下拉列表中选择列表项的值 - How to select a list item from a drop down by its value using Selenium Webdriver C# 使用硒(C#)从文本下拉菜单中选择一个项目 - Using Selenium (C#) Select an item from a drop down by text 如何从下拉列表中选择值(Selenium C#) - How to select value from the drop down list (Selenium C#) 在 Web 程序集中使用 Selenium 从 C# 的下拉菜单中选择项目 - 不起作用 - Select item from drop down menu in C# using Selenium in web assembly - doesn't work 如何使用Selenium C#在列表框中选择1个以上的项目? - How to select more then 1 item in listbox using selenium c#? 如何为下拉选择项c#分配值 - how to assign value for drop down select item c# 如何使用 Selenium WebDriver C# 从下拉列表中选择一个选项? - How to select an option from drop down using Selenium WebDriver C#? 使用C#的Selenium WebDriver:从下拉菜单中选择项(在IE中不起作用) - Selenium WebDriver with C#: select item from drop-down menu (doesn't work in IE) 无法从 Selenium C# webdriver 的多选下拉列表(组合框)中选择值 - Not able to select value from multi select drop down (Combo box) in Selenium C# webdriver 无法选择下拉值:jQuery,C#和Selenium - Can't Select drop down value: jQuery, C#, and Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM