University Help
University Help

Reputation: 139

C# Selenium sendkeys

I'm trying to submit a list of strings at once rather than individually via a text file, using sendkeys in selenium by findelement.

Example of usage:

 driver.FindElement(By.Name("search")).SendKeys(line);

What I'm using at the moment is a foreach loop to iterate through the list sending a request with the current line/string individually:

            foreach (String line in File.ReadAllLines(@"input.txt"))
            {
                string search = line;

                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
                IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("search")));

                if (driver.FindElements(By.Name("search")).Count != 0)
                {
                    driver.FindElement(By.Name("search")).SendKeys(search);
                }

                driver.FindElement(By.Name("srchbtn")).Click();

                Results();
            }

however the site allows upto 100 strings per search, so rather than sending 1 string each request, I would line to send 100.. How would I extract the first 100 strings from my text file and input them as a list into sendkeys, then the next 100 lines and so on..

I did try:

 var Lines = File.ReadLines(@"input.txt").Take(100).ToList();

 driver.FindElement(By.Name("search")).SendKeys(Lines);

but it returned a error stating:

 cannot convert from 'System.Collections.Generic.List<string>' to 'string'

Upvotes: 0

Views: 2164

Answers (1)

Ray Krungkaew
Ray Krungkaew

Reputation: 6965

Join the string before sending

var query = string.Join(" ", Lines);

driver.FindElement(By.Name("search")).SendKeys(query);

Upvotes: 3

Related Questions