我正在尝试在文本字段中输入字符,但它不起作用。下面我提到了2段代码。 JS 根本不起作用。在第一段代码中,单击有效,但其他步骤无效。 Xpath 是正确的,因为 click 正在处理该问题。
util.driver.findElement(By.xpath("//input[@id='input-1']")).click();
util.driver.findElement(By.xpath("//input[@id='input-1']")).clear();
util.driver.findElement(By.xpath("//input[@id='input-1']")).sendKeys("hjgfjg");
JavascriptExecutor js = (JavascriptExecutor) util.driver;
js.executeScript("document.getElementByXpath('//input[@id='input-1']').value = 'TEST')");
最佳答案
所需的元素是动态元素,因此要在该元素上调用 sendKeys()
,您必须为 elementToBeClickable()
引发 WebDriverWait您可以使用以下任一 Locator Strategies :
css选择器
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.slds-input[id^='input-'][aria-describedby^='help-message-']"))).sendKeys("hjgfjg");
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='slds-input' and starts-with(@id, 'input-')][starts-with(@aria-describedby, 'help-message-')]"))).sendKeys("hjgfjg");
关于java - 无法使用 selenium sendKeys 在文本字段中输入字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58209660/