The input operation of selenium to the text box generally has two forms. The traditional one is to directly input information in the text box through the sendKeys() method through the positioning element. But sometimes we can locate it by id, but we can't enter text information into the text box through sendKeys().
(Digression, bad luck, and encountered two bugs at the same time: 1. You can locate the input box but cannot enter data, 2. You can locate the button, but you can't do it)
In this case, the input also needs to be done with the help of JavaScript code.
HTML code
<textarea id="id" style="width: 98%" cols="50" rows="5" class="txtarea"> </textarea> ... ...
The first:
driver.findElement(By.id("id")).sendKeys("Content that needs to be entered");
1 public static void main(String[] args) throws InterruptedException { 2 System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe"); 3 ChromeOptions Options = new ChromeOptions(); 4 Options.addArguments("user-data-dir=C:\\Users\\happy\\AppData\\Local\\Google\\Chrome\\User Data"); 5 WebDriver driver = new ChromeDriver(Options); 6 driver.manage().window().maximize(); 7 try { 8 driver.get("file:///C:/Users/happy/Desktop/NewFile.html"); 9 driver.findElement(By.id("id")).sendKeys("What to enter"); 10 } finally { 11 Thread.sleep(5000 ); 12 driver.close(); 13 driver.quit (); 14 } 15 }
The second:
Execute JavaScript code via executeScript(). to implement the text box operation.
1 public static void main(String[] args) throws InterruptedException { 2 System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe"); 3 ChromeOptions Options = new ChromeOptions(); 4 Options.addArguments("user-data-dir=C:\\Users\\happy\\AppData\\Local\\Google\\Chrome\\User Data"); 5 WebDriver driver = new ChromeDriver(Options); 6 driver.manage().window().maximize(); 7 try { 8 driver.get("file:///C:/Users/happy/Desktop/NewFile.html"); 9 String text = "input text"; 10 String js = "var sum=document.getElementById('id'); sum.value='" + text + "';"; 11 ((JavascriptExecutor)driver).executeScript(js); 12 } finally { 13 Thread.sleep(5000); 14 driver.close(); 15 driver.quit(); 16 } 17 }