无法使用 selenium sendKeys 在文本字段中输入字符

我正在尝试在文本字段中输入字符,但它不起作用。下面我提到了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')");

https://img4.mukewang.com/650aaaa90001fc2210640183.jpg


猛跑小猪
浏览 91回答 3
3回答

慕容森

所需的元素是动态元素,因此要调用sendKeys()该元素,您必须引发WebDriverWait ,并且elementToBeClickable()可以使用以下任一定位器策略:cssSelector: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");

浮云间

尝试使用Actions:WebElement input = util.driver.findElement(By.xpath("//input[@id='input-1']"));Actions action = new Actions(util.driver);action.moveToElement(input).click().sendKeys("test").build().perform();导入后:import org.openqa.selenium.WebElement;import org.openqa.selenium.interactions.Actions;

撒科打诨

也许您可以尝试以下解决方法:尝试先单击文本框,然后调用 sendKeys()。当输入事件到达太快时,Angular 无法处理它们。作为解决方法,您需要发送单个字符,并且每个字符之间有较小的延迟。Selenium sendKeys 不发送所有字符您可以尝试使用 Actions 类。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java