0

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'

1 Answer 1

3

Join the string before sending

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

driver.FindElement(By.Name("search")).SendKeys(query);
Sign up to request clarification or add additional context in comments.

2 Comments

I need each string to be on a new line :/
replace " " with Environment.NewLine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.