简体   繁体   中英

Selenium C# dropdown list selecting values

I'm having problems with populating a drop down list with Selenium WebDriver on C#.

Here is the HTML code:

<div class="chosen-container chosen-container-single chosen-container-single-nosearch" style="width: 278px;" title="" id="phmain_0_phcontent_4_ddlJobType_chosen">
    <a tabindex="-1" class="chosen-single">
        <span>Fabricator</span>
        <div><b></b></div>
    </a>
    <div class="chosen-drop">
    <div class="chosen-search">
        <input type="text" autocomplete="off" readonly=""></div>
        <ul class="chosen-results">
            <li class="active-result" style="" data-option-array-index="1">Fabricator</li>
            <li class="active-result" style="" data-option-array-index="2">Postformer</li>
            <li class="active-result" style="" data-option-array-index="3">Retailer</li></ul>
        </div>
    </div>

SelectElement can not be used since there is no <select> tag.

I have tried with selecting the XPATH of the <li> element, without luck:

var jobTypeInput = Driver.Instance.FindElement(By.XPath("/html/body/form/div[4]/div[2]/section/div[2]/div[1]/div[3]/fieldset[1]/div[4]/div/div/ul/li[1]"));
jobTypeInput.Click();

Getting this error:

Unable to locate element: {"method":"xpath","selector":"/html/body/form/div[4]/div[2]/section/div[2]/div[1]/div[3]/fieldset[1]/div[4]/div/div/ul/li[1]"}

Any ideas on how to populate the drop down list with one of it's 3 values?

I can't directly answer why FindElement isn't getting the element, but I have a few recommendations.

is Xpath absolutely necessary here? What about using a CSS selector or searching by LinkText?

I would try doing:

Driver.Instance.FindElements(By.CssSelector(".active-result"))[1] ; 

Have you ever used the Chrome extension "Selector Gadget"? It's pretty great for things like this.

After using: in: 在:

var selected = Driver.Instance.FindElement(By.XPath("/html/body/form/div[4]/div[2]/section/div[2]/div[1]/div[3]/fieldset[1]/div[4]/div/div/ul/li[2]"));
selected.Click();
and:
var jobTypeInput = Driver.Instance.FindElements(By.Id("phmain_0_phcontent_4_ddlJobType_chosen"));
jobTypeInput.Click();

Thread.Sleep(3000);

var selected = Driver.Instance.FindElements(By.XPath("/html/body/form/div[4]/div[2]/section/div[2]/div[1]/div[3]/fieldset[1]/div[4]/div/div/ul/li[2]")); selected.Click();

you will always get:
 Cannot apply indexing with [] to an expression of type 'OpenQA.Selenium.IWebElement 
Try use:
 var jobTypeInput = Driver.Instance.FindElements(By.Id("phmain_0_phcontent_4_ddlJobType_chosen")); jobTypeInput.Click(); 

\n\n

Thread.Sleep(3000);

\n\n

var selected = Driver.Instance.FindElements(By.XPath("/html/body/form/div[4]/div[2]/section/div[2]/div[1]/div[3]/fieldset[1]/div[4]/div/div/ul/li[2]")); selected.Click();

I have managed to resolve the problem, with the following code:

var jobTypeInput = Driver.Instance.FindElement(By.Id("phmain_0_phcontent_4_ddlJobType_chosen"));
jobTypeInput.Click();

Thread.Sleep(3000);

var selected = Driver.Instance.FindElement(By.XPath("/html/body/form/div[4]/div[2]/section/div[2]/div[1]/div[3]/fieldset[1]/div[4]/div/div/ul/li[2]"));
selected.Click();

Basically I have selected the dropdown by ID and opened it. After that I just found the item I wanted to choose and selected it via xPath.

Maybe not the nicest solution, but it worked for me.

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