繁体   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

对于我在C#中使用selenium进行自动化测试,我想从类型字段(ref附加图像)中选择一个下拉列表项(any),但是由于所选元素的id / xpath是在每次加载页面时动态生成的,所以我选择了运行录制的脚本时,元素单击失败。

在此输入图像描述 例如,我的下面脚本失败,因为XPath“//选择[@ name ='条目[ db9ef219-0f54-4925-9589-0f39351f44a4 ] .TypeID']”每次在VS.Net中运行测试时都会更改。 每次页面加载时,值db9ef219-0f54-4925-9589-0f39351f44a4都会更改。

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

这是页面代码 - 在每个新行的加载时生成一个唯一值(低于它的c36582c1-131a-4f6f-8711-390048f5779f )并存储在类RegEffEntryContainer下 ,并用于每个列表元素 - 类型/描述(例如id =“Entries_c36582c1-131a-4f6f-8711-390048f5779f__TypeID”,id =“Entries_c36582c1-131a-4f6f-8711-390048f5779f__Organisation”)

在此输入图像描述

任何帮助解决这个问题将非常感谢 - 提前感谢!

仅供参考 :如果我使用动态XPath使用下面的代码,它只适用于第一行,我将无法通过脚本记录任何后续行条目。 要求是在单击“ 保存/提交”按钮之前输入所有3个主菜(上面的屏幕截图中未显示)。

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

感谢@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>

更新[27/02/2019 7:00 PM] - 因为用于TypeID / OrganisationID的数字ID( 值:21b28f6b-8aaa-4924-815a-1d925585fa36 )是在页面加载时动态创建的,有没有办法使用Javascript来实现在页面加载时将其记录在变量中并重新使用它以便稍后创建用于元素标识的XPath? 在此输入图像描述

如果您知道行的确切数量,那么只需使用它

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]"));

这可能不是一个完美的答案,或者是最好的方法,但我能够使用JavaScript解决这个问题。 使用Array在运行时存储动态ID,稍后使用它们创建字符串以在DOM上查找所需的元素。 以下是我的代码。

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.

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