我想从Drop Down中选择一个项目。但是在编译时,它无法找到元素。
<select name="ctl00$cphmain$ddlWorkstation" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphmain$ddlWorkstation\',\'\')', 0)" id="ctl00_cphmain_ddlWorkstation" class="dropdown_s" style="width:180px;">
<option value="Select" title="Select">Select</option>
<option selected="selected" value="2" title="Hospital A">Hospital A</option>
</select>
我写的代码是:
IWebElement facilityName = driver.FindElement(By.XPath("//select[@name='ctl00$cphmain$ddlWorkstation']"));
SelectElement select = new SelectElement(facilityName);
select.SelectByText("Hospital A");
我面临的错误:
Test method ChromeProject.Login.Chrome_Login threw exception:
OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@name='ctl00$cphmain$ddlWorkstation']"}
(Session info: chrome=58.0.3029.110)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64)
答案 0 :(得分:0)
您似乎正在尝试处理动态元素。您可能需要等待一段时间,直到select
中出现DOM
:
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
IWebElement facilityName = wait.Until(ExpectedConditions.ElementIsClickable(By.XPath("//select[@name='ctl00$cphmain$ddlWorkstation']")));
SelectElement select = new SelectElement(facilityName);
select.SelectByText("Hospital A");