Print all the Ajax auto-suggestions using Selenium C-Sharp
Open google website and type ‘hello’. A lot of auto-suggestive options would come up at run time (dynamic list)
Using Selenium-C#, we would be automating below 2 tasks:
a) print all the ajax auto-suggestions that come up;
b) click the auto-suggest option ‘hello in french’
To achieve these, let us inspect any auto-suggest option
This is an ‘li’ tag that is a child of ‘ul’ tag that further is a child of ‘div’ tag. The custom xpath of ‘Hello’ can be created as:
//div[@class='OBMEnb']/ul/li[1]
Similarly the xpath of other search results would be:
//div[@class='OBMEnb']/ul/li[2]
//div[@class='OBMEnb']/ul/li[3]
//div[@class='OBMEnb']/ul/li[4]
and so on…
So we notice that the integer is varying for each xpath:
//div[@class='OBMEnb']/ul/li[1]
//div[@class='OBMEnb']/ul/li[2]
//div[@class='OBMEnb']/ul/li[3]
……
If we remove this integer, we get below xpath that will fetch all the search results as shown below:
//div[@class='OBMEnb']/ul/li
So we can use this xpath to store all the results in a list and then loop through this list to print auto suggest list options, see lines 29-34 below
Notice line#20 above. We have added the implicit wait of 20 seconds for finding any element on the webpage. This is the default global wait time for which selenium waits to find the objects on the page. Now whenever you write driver.FindElements or driver.FindElement, this would be the default timeout. After 20 seconds, an exception would be thrown if the element is not found.
Run the script, notice that all the ajax options get printed in the output
Next, lines 32-33 will help click the desired option from the dynamic list
Run the script, notice below that ‘hello in french’ gets clicked
You can find the code snippet at the end.
Another example
Launch yahoo.com and type ‘hello’ in the search field
The below xpath highlights the first option ‘hello’
//div[@class='sa-tray']/ul/li[1]
Similarly below highlights the 3rd option
//div[@class='sa-tray']/ul/li[3]
Again, we see the varying integer:
//div[@class='sa-tray']/ul/li[1]
//div[@class='sa-tray']/ul/li[2]
//div[@class='sa-tray']/ul/li[3]
and so on….
If we remove this integer, we get below xpath that will fetch all the search results:
//div[@class='sa-tray']/ul/li
So let us write the below code. Notice line#31, this time we are using foreach loop
Run the script, notice below that all the options are printed
To click ‘hello’ from ajax list, we write the lines 34-35
Run the script, notice below that the script clicks ‘hello’ from the dropdown list
The code snippet is at the end.
Code snippet (google search)
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RelLoc
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
driver.Navigate().GoToUrl("https://www.google.com/"); driver.FindElement(By.XPath("//input[@name='q']")).SendKeys("hello");
Thread.Sleep(5000);
IList<IWebElement> ajaxOptions = driver.FindElements(By.XPath("//div[@class='OBMEnb']/ul/li"));
for (int i = 0; i < ajaxOptions.Count; i++)
{
Console.WriteLine(ajaxOptions[i].Text);
if (ajaxOptions[i].Text.Contains("hello in french"))
ajaxOptions[i].Click();
}
Console.ReadKey();
}
}
}
Code snippet (yahoo search)
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RelLoc
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
driver.Navigate().GoToUrl("https://www.yahoo.com/");
driver.FindElement(By.XPath("//input[@name='p']")).SendKeys("hello");
Thread.Sleep(5000);
IList<IWebElement> ajaxOptions = driver.FindElements(By.XPath("//div[@class='sa-tray']/ul/li"));
int count = ajaxOptions.Count;
Console.WriteLine(" Number of list options--> " +count);
foreach (var elem in ajaxOptions)
{
Console.WriteLine(elem.Text);
//if (elem.Text.Contains("hello"))
//elem.Click();
}
Console.ReadKey();
}
}
}
Thank you for reading!