C#Selenium JavaScriptExecutor无法设置或选择选项

时间:2019-03-14 19:49:57

标签: javascript c# selenium-webdriver

我有以下HTML:

<div class="k-list-scroller" unselectable="on" style="height: auto;">
    <ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" id="AdmittedFromId_listbox" aria-live="off" data-role="staticlist" role="listbox">
        <li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="0">Physician/Hm w/o HH</li>
        <li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">Ad Fr Hospital</li>
        <li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="2">Ad fr SNF</li>
        <li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="3">Admit fr OthFacility</li>
        <li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="4">Ad Court Ordered</li>
    </ul>
</div>

我尝试了以下方法:

IWebElement admittedFrom = Driver.ngInstance.FindElement(By.XPath("//input[@id='AdmittedFromId']"));
IJavaScriptExecutor admittedFromExecutor = (IJavaScriptExecutor)Driver.ngInstance;
admittedFromExecutor.ExecuteScript("arguments[0].click();", admittedFrom);
admittedFromExecutor.ExecuteScript("document.getElementByID('AdmittedFromId_listbox').data-offset-index='1';");
IWebElement admittedFrom = Driver.ngInstance.FindElement(By.XPath("//input[@id='AdmittedFromId']"));
IJavaScriptExecutor admittedFromExecutor = (IJavaScriptExecutor)Driver.ngInstance;
admittedFromExecutor.ExecuteScript("arguments[0].click();", admittedFrom);
admittedFromExecutor.ExecuteScript("arguments[0].setAttribute('data-offset-index',arguments[1])", admittedFrom, "1");

我已经尝试了几件事,但是无论尝试如何,我都无法设置该值。 任何帮助表示赞赏。预先感谢。

1 个答案:

答案 0 :(得分:0)

看起来您用于findElement的ID(“ AdmittedFromId”)与HTML中的ID(“ AdmittedFromId_listbox”)不匹配。