硒:-无法在文本框中写

时间:2020-02-06 09:30:04

标签: java selenium selenium-webdriver

click事件运行正常,但sendKeys事件无效。

我的代码是:-

 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");

我遇到错误:

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)

文本框的HTML是:-

<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>

1 个答案:

答案 0 :(得分:0)

所需的元素是Angular元素,因此要定位必须为elementToBeClickable引入 WebDriverWait 的元素,您可以使用以下任何一个{{3} }:

  • 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");