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);
1
  • Can you please share you'r html code ? Commented Oct 29, 2017 at 7:33

2 Answers 2

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')]

Sign up to request clarification or add additional context in comments.

1 Comment

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("phptravels.net"); WebElement e = driver.findElement(By.xpath("//*[@id='select2-search']/input")); e.click(); e.sendKeys(city);
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.