Reputation: 148
I'm trying to handle a auto suggestive dopdown , where in i enter a searchTerm results will be suggested by the term. I tried to loop it and check if the actual text is equal to expected text and if Yes clicking it
site:https://www.zoopla.co.uk/
This is the page i'm trying to handle it
When clicking it throws element not visible exception Is there any other way, auto suggestive dropdowns can be handled?
I'm using for loop to get the options from list and try to compare with the texts from the list and select it based on the true condition
Homepage.java
//selecting region
public void selectRegion(String regionName) throws InterruptedException {
int regionCount = getOptions().size();
System.out.println("count of region" + regionCount);
for(int i=0;i<getOptions().size();i++) {
String region = getOptions().get(i).getText();
System.out.println(regionName);
if(regionName.equals(regionName)) {
try {
getOptions().get(i).click();
break;
}
catch (Exception e) {
System.out.println("exception >>" + e.toString()) ; }
}
}
}
@Test
public void homePage() throws InterruptedException {
HomePage homePage = PageFactory.initElements(driver, HomePage.class);
homePage.EnterSearch("lond");
Thread.sleep(1000);
homePage.selectRegion("London");
homePage.clickSearchBtn();
The Desired option is getting selected , but in console , it throws repeated Element not visible exception
> exception >>org.openqa.selenium.ElementNotVisibleException: element not interactable (Session info: chrome=73.0.3683.86) (Driver info: chromedriver=73.0.3683.75,platform=Linux 4.15.0-47-generic x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-118
Upvotes: 0
Views: 157
Reputation: 7708
Refer below code :
HomePage
Initialize webelements :
@FindBy(css = "ul.ui-autocomplete>li>a")
List<WebElement> regions;
Method to select region :
public void selectRegion(String param) {
for (WebElement e : regions) {
if (e.getText().equals(param)) {
e.click();
break;
}
}
}
And this is your calling class
driver.get("https://www.zoopla.co.uk/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
HomePage page = new HomePage(driver);
page.enterSearch("lond");
page.selectRegion("London");
page.clickSearchBtn();
The code is working fine at my end in chrome browser let me know if you have any issue in this
Upvotes: 1
Reputation: 177
public void searchUsingAutoComplete(String element,String reuiredTextToClick) {
WebDriverWait wait = new WebDriverWait(d,30);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(element)));
List<WebElement> list=d.findElements(By.xpath(element));
log.info("Auto Suggest List ::" + list.size());
int i;
for( i = 0 ;i< list.size();i++)
{
log.info(list.get(i).getText());
if(reuiredTextToClick.equalsIgnoreCase(list.get(i).getText()))
{
list.get(i).click();
break;
}else {
log.info(list.get(i).getText()+" not equals "+reuiredTextToClick);
}
if(i==list.size()-1) {log.error(reuiredTextToClick+" not found");
throw new ElementNotVisibleException("Element not found with "+ reuiredTextToClick+" text");
}
}
Use the above code your element will be:- //ul[@id='ui-id-1']//li , I tried with this code I'm able to select the required text
Upvotes: 1