【问题标题】:I want to select an item in a dropdown list but the id of the dropdown list change with every run我想在下拉列表中选择一个项目,但下拉列表的 id 每次运行都会改变
【发布时间】:2021-12-16 20:38:05
【问题描述】:

我想在下拉列表中选择一个项目,但下拉列表的 id 每次运行都会改变。这是元素代码:<ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" id="TableName_c2937ec1-50e5-4df6-b2e1-8a9c0a2b3f9f_listbox" aria-live="polite" data-role="staticlist" role="listbox"><li tabindex="-1" role="option" unselectable="on" class="k-item" aria-selected="false" data-offset-index="0">CaseCmsCustomerDto</li>

我的代码

IWebElement fltTableNm = _driver.FindElement(By.XPath("/html/body/div[4]/div/div/div/div[1]/div/div/div[1]/div/div[3]/table/tbody/tr[2]/td[2]/fieldset[2]/div[1]/div[1]/div/span/span"));
            fltTableNm.Click();
            Thread.Sleep(1000);
            IWebElement slctfltTableNm = _driver.FindElement(By.XPath("//id=[starts-with(.,'TableName')]/li[1]"));
            slctfltTableNm.Click();

【问题讨论】:

    标签: c# selenium-chromedriver specflow


    【解决方案1】:

    这个 xpath 怎么样?你的想法是对的,id 是动态的,但包含一些足够独特的东西。 您的 xpath 错过了 //ul 并以这样的方式开始:

    IWebElement slctfltTableNm = _driver.FindElement(By.XPath("//ul[starts-with(@id,'TableName')]/li[1]")); 
    

    或者 By.XPath("//ul[@class='k-list k-reset']/li[1]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      相关资源
      最近更新 更多