J. Doem
J. Doem

Reputation: 649

How to handle auto suggestion web element in selenium 3

I am new on automation testing and have difficulty when trying to pratice using selenium 3 on booking.com website

There is auto suggestion text box, when you type word, shown auto suggestion and you can click from the list i.e Downtown Singapore

enter image description here

Have try with xpath id("basiclayout")/div[@class="leftwide rilt-left"]/div[@class="sb-searchbox__outer"]/form[@id="frm"]/div[@class="sb-searchbox__row u-clearfix"]/div[1]/div[@class="c-autocomplete sb-destination"]/ul[@class="c-autocomplete__list sb-autocomplete__list -visible"]/li[@class="c-autocomplete__item sb-autocomplete__item sb-autocomplete__item--city sb-autocomplete__item__item--elipsis"]

or css c-autocomplete__item sb-autocomplete__item sb-autocomplete__item--city sb-autocomplete__item__item--elipsis

all scenario failed when i run my testcases on selenium java

How to handle on such web element?

Complete code:

public class Selenium3Testing {
private WebDriver driver;

@Before
public void setUp() {
    String baseUrl = "https://www.booking.com/";
    System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver.exe");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    driver = new ChromeDriver(capabilities);
    driver.get(baseUrl);
}

@After
public void tearDown() {
    driver.quit();
}

@Test
public void openBookingDotCom() {
    driver.findElement(By.id("ss")).click();
    driver.findElement(By.id("ss")).clear();
    driver.findElement(By.id("ss")).sendKeys("Singapore");
    //click on auto suggestion row number 2
    driver.findElement(By.css("c-autocomplete__item sb-autocomplete__item sb-autocomplete__item--city sb-autocomplete__item__item--elipsis")).click();
    } 
}

Upvotes: 0

Views: 984

Answers (1)

murali selenium
murali selenium

Reputation: 3927

I just typing from mobile, so no code, here the way we can do it.

For giving input to input box, I hope if we pass total word in sendkeys, suggestion's may not load or delayed. So best way I follow is pass each character..may be sleep say 300 milli sec for each char. Write as small method which will loop for all chars in word.

To click on suggestion list, try for xpath contains text..or any one works well.

Upvotes: 1

Related Questions