1

phptravels location text field

Have attached a image with Html code. I couldn't able to write a xpath to recognise and sendkeys. The text field has different xpath when it is focused and when it is not. Its throwing me NoSuchElementFound exception.

driver.get("http://www.phptravels.net");
WebElement e = driver.findElement(By.xpath("//*[@id='select2-search']/input"));
     e.click();
     e.sendKeys(city);

2 Answers2

0

You can create xpath using other attributes of that element like

//input[contains(@attribute,'attribute_value')]

Eg. For a input element with class attribute "textbox", xpath can be written like

//input[contains(@class,'textBox')]

Arjun
  • 1
  • 3
  • I wrote an xpath using class attribute, but its throwing me an NosuchElementFound exception. Moreover the text field has different xpath when it is focused . driver.get("http://www.phptravels.net"); WebElement e = driver.findElement(By.xpath("//*[@id='select2-search']/input")); e.click(); e.sendKeys(city); – Sowjanya Pratap Oct 29 '17 at 16:18
0

I tried with below two points and it worked for me, 1) modified xpath with span text() 2) Instead of e.sendKeys(), tried actions.sendKeys() as the former throwing 'cannot focus' error. Credit to link

        driver.get("http://www.phptravels.net");
        WebElement e = driver.findElement(By.xpath("//span[text()='Search by Hotel or City Name']"));
/*      e.click();
        e.sendKeys("GOA");
*/  
        Actions actions = new Actions(driver);
        actions.moveToElement(e);
        actions.click();
        actions.sendKeys("GOA");
        actions.build().perform();
prashanth
  • 66
  • 5