无法使用硒将值发送到输入框

Not able to send values to the input box using selenium

我正在尝试启动一个简单的应用程序并尝试将字符串发送到输入框。但是当我尝试这样做时,我得到 no such element: Unable to locate element: method":"xpath","selector":"//input"}。但它是没有复杂结构的简单 DOM。有人可以帮我解决这个问题吗?

package selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {
       public static void main(String[] args) {
            
            //Creating a driver object referencing WebDriver interface
            WebDriver driver;
            
            //Setting webdriver.gecko.driver property
            System.setProperty("webdriver.chrome.driver",  "E:\chromedriver.exe");
            
            //Instantiating driver object and launching browser
            driver = new ChromeDriver();
            
            //Using get() method to open a webpage
            driver.get("https://todomvc.com/examples/angular2/");
            driver.findElement(By.xpath("//input")).sendKeys("amazon");
            //Closing the browser
            driver.quit();
     
        }
}


您可以尝试 显式等待 :

Explicit waits are available to Selenium clients for imperative, procedural languages. They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.

代码:

driver.get("https://todomvc.com/examples/angular2/");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input"))).sendKeys("Amazon");

详细了解显式等待 here