Selenium Webdriver sendKeys 在 IE11 32 位中输入值,但随后将其删除
Selenium Webdriver sendKeys enters values in IE11 32 bit but then removes it
我必须在 java 的 selenium 测试中的输入框中输入文本。我正在使用下面的代码来执行此操作,它输入字符但随后将其删除:
WebElement depart=webControls.getDriver().findElement(By.id("oneWayFlight_fromLocation"));((JavascriptExecutor) webControls.getDriver()).executeScript("document.getElementById('oneWayFlight_fromLocation').value='JFK'");
或
((JavascriptExecutor) webControls.getDriver()).executeScript("arguments[0].value='JFK';",depart);
或
((JavascriptExecutor) webControls.getDriver()).executeScript(String.format("document.getElementById('oneWayFlight_fromLocation').value='JFK';","JFK"));
这是文本字段:
<input id="oneWayFlight_fromLocation" type="text" class="InputText-control hasError hasIcon" name="oneWayFlight_fromLocation" placeholder="From" autocomplete="off" value="">
我遵循以下设置:
- Windows 10 64 位
- IE11 32 位
- IE DriverServer 32 位
- 所有人的保护模式都已关闭
- nativeEvents 为假
- REQUIRE_WINDOW_FOCUS 是真的
- 64 位进程在 Advance Security 中未选中
我建议尝试使用 Sendkeys() 进行测试。我试着在我这边测试它,它没有任何问题。
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");
}
输出:
此外,您可以修改代码以根据您的要求工作。
使用[=17将字符序列发送到FROM和TO字段=]:
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
:
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");
浏览器快照:
我必须在 java 的 selenium 测试中的输入框中输入文本。我正在使用下面的代码来执行此操作,它输入字符但随后将其删除:
WebElement depart=webControls.getDriver().findElement(By.id("oneWayFlight_fromLocation"));((JavascriptExecutor) webControls.getDriver()).executeScript("document.getElementById('oneWayFlight_fromLocation').value='JFK'");
或
((JavascriptExecutor) webControls.getDriver()).executeScript("arguments[0].value='JFK';",depart);
或
((JavascriptExecutor) webControls.getDriver()).executeScript(String.format("document.getElementById('oneWayFlight_fromLocation').value='JFK';","JFK"));
这是文本字段:
<input id="oneWayFlight_fromLocation" type="text" class="InputText-control hasError hasIcon" name="oneWayFlight_fromLocation" placeholder="From" autocomplete="off" value="">
我遵循以下设置:
- Windows 10 64 位
- IE11 32 位
- IE DriverServer 32 位
- 所有人的保护模式都已关闭
- nativeEvents 为假
- REQUIRE_WINDOW_FOCUS 是真的
- 64 位进程在 Advance Security 中未选中
我建议尝试使用 Sendkeys() 进行测试。我试着在我这边测试它,它没有任何问题。
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");
}
输出:
此外,您可以修改代码以根据您的要求工作。
使用[=17将字符序列发送到FROM和TO字段=]:
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
: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");
浏览器快照: