[英]Selenium Webdriver sendKeys enters values in IE11 32 bit but then removes it
I have to enter text in a input box in a selenium test in java.我必须在 java 的 selenium 测试的输入框中输入文本。 I am using below code to do that and it enters the characters but then deletes it:
我正在使用下面的代码来执行此操作,它输入字符但随后将其删除:
WebElement depart=webControls.getDriver().findElement(By.id("oneWayFlight_fromLocation"));((JavascriptExecutor) webControls.getDriver()).executeScript("document.getElementById('oneWayFlight_fromLocation').value='JFK'");
OR或者
((JavascriptExecutor) webControls.getDriver()).executeScript("arguments[0].value='JFK';",depart);
OR或者
((JavascriptExecutor) webControls.getDriver()).executeScript(String.format("document.getElementById('oneWayFlight_fromLocation').value='JFK';","JFK"));
Here is the text field:这是文本字段:
<input id="oneWayFlight_fromLocation" type="text" class="InputText-control hasError hasIcon" name="oneWayFlight_fromLocation" placeholder="From" autocomplete="off" value="">
I following setup:我遵循设置:
To send a character sequence to the FROM and TO field using Selenium WebDriver you don't have to resort to JavascriptExecutor 's executeScript()
method.要使用Selenium WebDriver将字符序列发送到FROM和TO字段,您不必求助于JavascriptExecutor的
executeScript()
方法。 Instead you can use the much proven and efficient sendKeys()
method inducing WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies :相反,您可以使用经过验证且有效的
sendKeys()
方法为elementToBeClickable()
引入WebDriverWait ,并且您可以使用以下任一Locator Strategies :
cssSelector
: cssSelector
:
WebDriver driver = new InternetExplorerDriver(); driver.get("https://www.amextravel.com/flight-searches"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='One Way']"))).click(); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#oneWayFlight_fromLocation"))).sendKeys("JFK");
xpath
: xpath
:
WebDriver driver = new InternetExplorerDriver(); driver.get("https://www.amextravel.com/flight-searches"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='One Way']"))).click(); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='oneWayFlight_fromLocation']"))).sendKeys("JFK");
Browser Snapshot:浏览器快照:
I suggest trying to make a test with Sendkeys().我建议尝试使用 Sendkeys() 进行测试。 I try to test it on my side and it works without any issue.
我尝试在我这边测试它,它可以正常工作。
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver","D:\\D drive backup\\selenium web drivers\\IEDriverServer.exe");
WebDriver browser = new InternetExplorerDriver();
browser.get("https://www.amextravel.com/flight-searches");
browser.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebElement txtbox1=browser.findElement(By.name("oneWayFlight_fromLocation"));
txtbox1.sendKeys("ABC");
WebElement txtbox2 = browser.findElement(By.name("oneWayFlight_toLocation"));
txtbox2.sendKeys("xyz");
}
Output: Output:
Further, you can modify the code to work as per your requirements.此外,您可以修改代码以根据您的要求工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.