How you handle this depends on how it was implemented. For example, if
I execute the following:
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import
org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Test {
static String seleniumServer = "darrell-win7";
static String seleniumPort = "4444";
static String page = "
http://www.google.ca";
public static void main(String[] args) throws MalformedURLException {
URL hub = new URL("http://" + seleniumServer + ":" + seleniumPort +
"/wd/hub");
Capabilities dc = DesiredCapabilities.internetExplorer();
WebDriver driver = new RemoteWebDriver(hub, dc);
driver.get(page);
WebElement w = driver.findElement(By.name("q"));
w.sendKeys("sc");
WebElement t = driver.findElement(By.className("gssb_m"));
List<WebElement> ss = t.findElements(By.tagName("span"));
Iterator<WebElement> i = ss.iterator();
while(i.hasNext()) {
WebElement s = i.next();
System.out.println(s.getText());
}
driver.close();
}
}
It will print out the suggestions for me entering "sc" into the Google
search. The reason it works is because it does the following:
- find the input field
- enter "sc" into the input field
- this should trigger the auto-complete
- search for the table with class "gssb_m" because this is the table
which holds the suggestions
- find all the SPAN elements in the table because the text for the
suggestions are in the SPAN tags.
Where as if I try to test the auto-complete at Kobo Books I have to do
the following:
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import
org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Test {
static String seleniumServer = "darrell-win7";
static String seleniumPort = "4444";
static String page = "
http://www.kobobooks.com";
public static void main(String[] args) throws MalformedURLException {
URL hub = new URL("http://" + seleniumServer + ":" + seleniumPort +
"/wd/hub");
Capabilities dc = DesiredCapabilities.internetExplorer();
WebDriver driver = new RemoteWebDriver(hub, dc);
driver.get(page);
WebElement w = driver.findElement(By.name("q"));
w.sendKeys("sc");
WebElement t = driver.findElement(By.cssSelector("ul[class~=ui-
autocomplete]"));
List<WebElement> ss;
do {
ss = t.findElements(By.tagName("li"));
} while(ss.size() == 0);
Iterator<WebElement> i = ss.iterator();
while(i.hasNext()) {
WebElement s = i.next();
System.out.println(s.getText());
}
driver.close();
}
}
The differences are:
- when I enter text into the input box there is a delay before the
auto-complete gets populated. So I have to have a do-while loop to
wait for the unordered list to get populated.
- the list of suggestions are placed in an unordered list (UL) rather
than a table. So I need to find all the LI elements and print their
text.
For your implementation simulate a human. A real user would type a few
characters, wait for the auto-complete then select from the list of
suggestions.
On Jul 8, 4:00 am, B Chandra <
shekhar90...@gmail.com> wrote:
> I am facing some problem with autocomplete feaure of JS.
> Main scenario is when I provide some input to one of the textfield on
> webpage which shows possible results, I have to select one of the location
> from list appeared. (Scenario is same as when you type query in Google,
> possible results appears and here I have to do main job. From list of
> possible results, i have to select one result)
> How can I achieve this scenario?
>
> --
>
> Best regards,
> *B Chandra*