Problem in handling auto suggestion

Hi All,

I was writing a webdriver code to handle auto suggestion (monsterindia.com) for the homa page “keywork” field. but it is unable to locate the suggested element..please help me…

package Simple;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class AutoSuggestion {
@Test
public void select() throws InterruptedException
{
WebDriver d = new FirefoxDriver();
d.manage().window().maximize();
d.get(“http://www.monsterindia.com/“);
d.findElement(By.cssSelector(“‪#‎fts_id‬“)).sendKeys(“soft”);
Actions a = new Actions(d);

WebElement w = d.findElement(By.cssSelector(“‪#‎ui‬-id-4″));//this line is unable to locate element.
a.moveToElement(w).build().perform();
Thread.sleep(5000);
a.click(w).build().perform();

}

}

Train Asked on November 15, 2014 in Selenium WebDriver.

Hello,

After running your code I am getting below error message.

Note: Please let us know what you are trying to do here OR give a steps.


Unable to locate element: {"method":"css selector","selector":"?#?fts_id?"}

Command duration or timeout: 150 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'

on November 16, 2014.

@Sachin: correct, code is not working.

Action should not use here, key, mouse actions are different and webdriver actions. :).

Up voted for your solution.

on November 16, 2014.
Add Comment
  • 3 Answer(s)

    Hi,

    Please use below code, if not worked then let me know, if worked then ACCEPT the answer 🙂 .

    
    import java.util.List;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.WebElement;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    import org.openqa.selenium.support.ui.ExpectedConditions;
    
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    import org.testng.annotations.Test;
    public class Auto_Suggestions {
    
    @Test
    
    public void select() throws InterruptedException
    
    {
    
    WebDriver d = new FirefoxDriver();
    
    d.manage().window().maximize();
    
    d.get("http://www.monsterindia.com/");
    d.findElement(By.id("fts_id")).sendKeys("software");
    WebElement link = d.findElement(By.id("ui-id-1"));
    
    List<WebElement> links = link.findElements(By.tagName("li"));
    
    for(int i=0; i<links.size();i++)
    
    {
    
    System.out.println(links.get(i).getText());
    
    }
    
    WebElement myDynamicElement = (new WebDriverWait(d, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("ui-id-11")));
    
    d.findElement(By.id("ui-id-11")).click();
    
    d.findElement(By.className("mn_findbtn")).click();
    }
    
    }
    
    
    Vice Professor Answered on November 16, 2014.
    Add Comment

    HId.manage().window().maximize();
    d.get(“http://www.monsterindia.com/“);
    d.findElement(By.cssSelector(“‪#‎fts_id‬“)).sendKeys(“soft”);

    Thread.sleep(5000);    // as it will take some time  to appear suggestions
    Actions a = new Actions(d);

    WebElement w = d.findElement(By.cssSelector(“‪#‎ui‬-id-4″));//this line is unable to locate element.

    Vice Professor Answered on November 16, 2014.

    Thanks ankit…

    on November 16, 2014.

    @snpanigrahi9 :Why you are using Actions here?, for any purpose?. Can’t we do in other way?.

    on November 16, 2014.

    Please go through this link and will come to know the difference:ACTIONS

    on November 16, 2014.
    Add Comment

    One Way:
    You should include explicit wait statement

    Thread.sleep(5000)

    Another Way:
    Use Webdriver wait it will wait until the predicate isTrue or until the time Out expires.

    webDriverWait = (new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(final By locator)));

    In Real time avoid thread.sleep because its not good way. In case of webdriver wait there is polling happen so its good way.

    Train Answered on November 17, 2014.

    Yes, correct.

    on November 18, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.