简体   繁体   中英

How to select dropdown menu item which is defined under <span> tag using Selenium WebDriver and C#

 <div class="span3"> <div> LSP Account<br> <span title="" class="k-widget k-dropdown k-header" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="cboAccountSettings_listbox" aria-disabled="false" aria-busy="false" aria-activedescendant="7245f0ab-5ba4-4c7b-b722-6c204d700e9f" style="width: 98%;"> <span unselectable="on" class="k-dropdown-wrap k-state-default"> <span unselectable="on" class="k-input">Any</span> <span unselectable="on" class="k-select"> <span unselectable="on" class="k-icon ki-arrow-s">select</span></span></span> <select id="cboAccountSettings" style="width: 98%; display: none;" data-value-primitive="true" data-bind="value: SWO_Id" data-role="dropdownlist"> <option value="" selected="selected">Any</option> <option value="SWO-ARG">SoftwareONE ARG</option> <option value="SWO-AUS">SoftwareONE AUS</option> <option value="SWO-BOL">SoftwareONE BOL</option> <option value="SWO-BRA">SoftwareONE BRA</option> <option value="SWO-CAN">SoftwareONE CAN</option> <option value="SWO-CHL">SoftwareONE CHL</option> <option value="SWO-COL">SoftwareONE COL</option> <option value="SWO-CRI">SoftwareONE CRI</option> <option value="SWO-DOM">SoftwareONE DOM</option> <option value="SWO-ECU">SoftwareONE ECU</option> <option value="SWO-EMEA">SoftwareONE EMEA</option> <option value="SWO-GTM">SoftwareONE GTM</option> <option value="SWO-HKG">SoftwareONE HKG</option> <option value="SWO-HND">SoftwareONE HND</option> <option value="SWO-IDN">SoftwareONE IDN</option> <option value="SWO-JAM">SoftwareONE JAM</option> <option value="SWO-JPN">SoftwareONE JPN</option> </select> </span> </div> </div> 

在此处输入图片说明

1) I was able to click on the tag using, but not able to select the item:

driver.FindElement(By.CssSelector("span[aria-owns='cboAccountSettings_listbox']")).Click();

2) I used this code also, but not able to find the specific element:

var LSPAccount = WebBrowser.FindElement(By.CssSelector("span[aria-owns='cboAccountSettings_listbox']"));   
Thread.Sleep(1000);
var selectElement = new SelectElement(LSPAccount);
Thread.Sleep(1000);
selectElement.SelectByValue("SWO-ARG");

3) I also tried this , but no success:

var xpath = "//span[contains(@class,'k-widget k-dropdown k-header')][contains(@id,'cboAccountSettings')][contains(text(),'SoftwareONE ARG')]";
var admi = WebBrowser.FindElement(By.XPath(xpath));
Thread.Sleep(1000);
admi.Click();

You can try following :

//get the select html element
var selectList = driver.FindElement(By.Selector("select#cboAccountSettings"));
//wrap it inside webdriver's SelectElement and select option with value "SWO-ARG"
var selectElement = new SelectElement(selectList);
selectElement.SelectByValue("SWO-ARG");

SelectElement expects a IWebElement that represents a html select. Also, Please check that target select element is not inside some frame. In that case switch to target frame first.

Select element is not visible that's the problem. You can use javascript to select option (Java):

String option = "SWO-ARG";
((JavascriptExecutor) driver).executeScript("arguments[0].selected='selected';", driver.findElement(By.cssSelector("#cboAccountSettings option[value='" + option + "']")));

Try also this:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute('selected');", driver.findElement(By.cssSelector("#cboAccountSettings option[selected]")));
js.executeScript("arguments[0].setAttribute('selected', 'selected');", driver.findElement(By.cssSelector("#cboAccountSettings option[value='SWO-ARG']")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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