0

I am trying to select an element in a drop down list. When I tried using Selenium C# only I was getting a Div error and was advised to use jQuery to do the select.The Div error is Element should have been select but was div. I have tried using sendkeys to select dropdown, bu then i get the error it cannot focus on element.

I currently have the following code, but I am not able to get it running:

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using WebAuto;
using System.Drawing.Imaging;
using OpenQA.Selenium.Support.UI;
using Selenium.WebDriver.Extensions.JQuery;
using OpenQA.Selenium.Remote;

namespace WebAuto
{
    class AddTrade
    {
        [Test]
        public static void AddTrade1()
        {
            var driver = new ChromeDriver();
            driver.Navigate().GoToUrl("webaddress");
            var inputtext1 = driver.FindElement(OpenQA.Selenium.By.Id("lgLogin_txtUserId"));

            inputtext1.SendKeys("User");
            var inputpassword1 = driver.FindElement(OpenQA.Selenium.By.Id("lgLogin_txtPassword"));

            inputpassword1.SendKeys("Password");
            var inputbutton1 = driver.FindElement(OpenQA.Selenium.By.Id("btnLoginClient"));
            inputbutton1.Click();


            System.Threading.Thread.Sleep(3000);
            driver.FindElement(OpenQA.Selenium.By.Id("s2id_selTrader")).Click();

            $("#s2id_selTrader").val("Main Trader");

How can I fix this problem?

hthomas
  • 23
  • 7
  • 1
    What's the "Div error" you were getting? I'd recommend taking the time to figure out why your first approach didn't work in the first place because it's probably an indication that there's a problem which will come back to haunt you in other ways if it isn't addressed. Do you have multiple elements with the same ID on your page? Do you need to use a [WebDriverWait](https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_WebDriverWait.htm) (*not* `Thread.Sleep`) to allow the element time to load? etc. – StriplingWarrior Jan 13 '17 at 17:14
  • You could probably rewrite the title to briefly describe your question. Listing 3 technologies doesn't describe what the question is about. – Broots Waymb Jan 13 '17 at 17:29
  • @ StriplingWarrior: I get the error Element should have been select but was div – hthomas Jan 13 '17 at 18:36

3 Answers3

1

In order to run JavaScript using Selenium, you need to use the IJavaScriptExecutor of the driver. For example:

((IJavaScriptExecutor)driver).ExecuteScript("$('.toTop').remove();");

However, I don't know that that is the best way to select an item from a drop down. Have you tried casting the element as a SelectElement and using one of the methods within that class:

   SelectElement select = new SelectElement(element);

   select.SelectByValue("123");

There's also SelectByText and SelectByIndex.

MikeS
  • 1,734
  • 1
  • 9
  • 13
0

You can try adding a wait clause:

wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
if (!driver.FindElement(By.CssSelector("selector")).Displayed)
{
    wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("selector")));
    driver.FindElement(By.CssSelector("selector")).Text; // There are other properties and methods if you need
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eg_dac
  • 701
  • 4
  • 14
0

If i'm not mistaken, these selects are not real html select elements (and consequently, you can't use Selenium's SelectElement with them), but rather some scripted div elements that you need to interact with differently.

If you want to reproduce an actual user's experience, you should try something like this

    public void SetDropdownValue(IWebElement dropdown, string value)
    {
        By valueXpath = By.XPath("//div[@id='select2-drop']//li[div[text()='" + value + "']]");
        dropdown.FindElement(By.XPath(".//b[@role='presentation']")).Click();

        new WebDriverWait(Driver, TimeSpan.FromSeconds(1)).Until(ExpectedConditions.ElementExists(valueXpath));
        Driver.FindElement(valueXpath).Click();
    }

Be aware that, depending on the conditions, the item may not become visible at all (and that will throw an ElementNotVisibleException), so you may need to try bringing the element into view before clicking it. If that's the case, you can try either one of the methods described here, and add it after checking for its existence.

Also, check the "valueXpath" model I provided, and see if it is equal to the ones in your page. The li may contain a elements inside of it instead, so you have to locate the element that has the desired option text directly inside of it.

Community
  • 1
  • 1
Rafael Oliveira
  • 277
  • 1
  • 16