0
votes

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?

3
What driver are you using? PhantomJS? - SILENT
This function looks like it uses javascript to run. You can't select li. You could probably just copy a chosen li's text to input. - SILENT
The selector is extremely brittle and seems copied & pasted. This is problem number one. - Arran
@SILENT I'm using Selenium WebDriver. Can you please explain "just copy a chosen li's text to input"? Thank you - Edin Cenanovic

3 Answers

1
votes

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.

0
votes

After using: FindElement 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();
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();
Cannot apply indexing with [] to an expression of type 'OpenQA.Selenium.IWebElement
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();

-1
votes

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.