0

The click Event is working fine but sendKeys event is not.

My Code is:-

 driver.findElement(By.id("radio-1-4")).click();
 jse.executeScript("scroll(0, 500);");
 System.out.println("Authority Filter");
 driver.findElement(By.xpath("//*[@class=\"filter-label-text\" and text()= 'Authority']")).click();
 Thread.sleep(3000);
 //driver.findElement(By.xpath("//label[@class=\"control-label\" and @for='tfid-665-0']//parent::div[1]/child::input")).sendKeys("Public Work Department");
 WebElement element = driver.findElement(By.id("tfid-602-0"));
 element.click();
 element.sendKeys("public");

I am getting error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#tfid\-602\-0"}
(Session info: chrome=79.0.3945.130)

The HTML of the textbox is:-

<div class="form-group form-group-depth-1 form-group-search">
    <label for="tfid-664-0" class="control-label">
    </label>
    <input type="text" placeholder="Search for Authorities" id="tfid-664-0" name="search" class="form-control" value="">
</div>
Guy
  • 34,831
  • 9
  • 31
  • 66
RAHUL
  • 349
  • 1
  • 5
  • 14
  • 1
    The click doesn't work, the program never reached it. Since you have sleep I'm guessing the element is inside ` – Guy Feb 06 '20 at 09:33
  • Nope, Even after removing sleep function it's not working.. In this case i am even unable to find the exact XPath of the text box. – RAHUL Feb 06 '20 at 09:46
  • Could you please edit your post to show the HTML for ID="tfid-602-0" (currently you have Selenium looking for tfid-602-0, and showing HTML for tfid-664-0). – Simon Feb 06 '20 at 09:47
  • 1
    @RAHUL The point was that because you have sleep it's probably not a timing issue. Check if the element is inside ` – Guy Feb 06 '20 at 09:51
  • Now i have just used By.name("search") Error is coming as :- Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable Struck here..!! – RAHUL Feb 06 '20 at 10:06

1 Answers1

0

The desired element is an Angular element so to locate the element you have to induce WebDriverWait for the elementToBeClickable and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.form-group-search>label.control-label[for^='tfid']"))).sendKeys("public");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='form-control' and @placeholder='Search for Authorities']//preceding::label[1]"))).sendKeys("public");
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217