Please refer screenshot below.
 
 I am trying to provide value in dropdown field and select the very fisrt value. But it is not populating.
Kindly advise
I am trying to provide value in dropdown field and select the very fisrt value. But it is not populating.
Kindly advise
            Asked
            
        
        
            Active
            
        
            Viewed 124 times
        
    3 Answers
0
            
            
        Please Try in this way
var ul = driver.findelement(By.XPath("//ul[contains(@class,'your class name')]")).ToList();//loop over here. or if you know index than use it directly.
                var Select = ul[your index].FindElements(By.TagName("li")).ToList();
                foreach (IWebElement item in Select)
                {
                    if (item.Text.Trim() == "Your Text")
                    {
                        DateTime start = DateTime.Now;
                        while ((DateTime.Now - start).TotalMilliseconds < 1500)
                        {
                             Application.DoEvents();
                             System.Threading.Thread.Yield();
                        }
                        item.Click();
                        break;
                    }
                }
i hope it will help you.
 
    
    
        Amar Patel
        
- 39
- 5
- 
                    Thanks you so much!!! Above worked for me, but it works only while debugging. When i run the project it doesnot populate the value from dropdown. Am i missing anything? – Nikita Jul 02 '20 at 16:52
- 
                    var ul = driver.findelement(By.XPath("//ul[contains(@class,'your class name')]")).ToList(); var Select = ul[0].FindElements(By.TagName("li")).ToList(); foreach (IWebElement item in Select) { if (item.Text.Trim() == "TE_GL1") { item.Click(); // WebExtention.Wait(500); I am using Chrome driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50); break; } } – Nikita Jul 02 '20 at 16:56
- 
                    Also i get OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document at if statement – Nikita Jul 02 '20 at 18:13
- 
                    @neeta Jadhav i thing you need to use WebExtention.Wait(1500) before click() – Amar Patel Jul 03 '20 at 04:57
- 
                    and you can handle this StaleElementReferenceException: error using try{}catch(NoSuchElementException){ goto your step} and you can go further on error line using goto. i hope you understand. – Amar Patel Jul 03 '20 at 05:03
- 
                    What is this WebExtension. I cannot use it my code. Do we need to refer any namespace? – Nikita Jul 03 '20 at 09:27
- 
                    No, You can not use in your code. this is one class i created in my code and wait is a method of class . – Amar Patel Jul 03 '20 at 12:17
- 
                    You can use this like DateTime start = DateTime.Now; while ((DateTime.Now - start).TotalMilliseconds < 1500) { //Application.DoEvents(); System.Threading.Thread.Yield(); } – Amar Patel Jul 03 '20 at 12:18
0
            
            
        Try this one when you open your dropdown.
  Driver.FindElements(By.CssSelector(".col-xs-3")).First(e => e.Text = "TE_GL1").Click();
Or just click first element
  Driver.FindElements(By.CssSelector(".col-xs-3")).First().Click();
-1
            
            
        - 
                    5If you have to use this trick it often means the DOM is not completely loaded yet. Instead of Sleep you better `Wait` for an element to appear. see https://stackoverflow.com/a/7312740/578411 – rene Jul 04 '20 at 09:56
 
     
    