2017-05-12 2 views
1

ドロップダウンから項目を選択したいとします。しかし、コンパイルすると、要素を見つけることができません。XPath - Visual Studioを使用したドロップダウンオプションの選択

<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

あなたが動的な要素を処理しようとしているように思えます。あなたはDOMselect現在までいくつかの時間を待つ必要があるかもしれません:

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"); 
+0

それは「待つ」を追加することに取り組みました。ありがとう、トン! –

関連する問題