1

我必须在 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="">

我遵循设置:

  1. 视窗 10 64 位
  2. IE11 32 位
  3. IE DriverServer 32 位
  4. 保护模式全部关闭
  5. nativeEvents 为假
  6. REQUIRE_WINDOW_FOCUS 为真
  7. 高级安全中未选中的 64 位进程

在此处输入图像描述

4

2 回答 2

1

要使用Selenium WebDriver将字符序列发送到FROMTO字段,您不必求助于JavascriptExecutor的方法。相反,您可以使用经过验证且有效的方法来诱导WebDriverWait,并且您可以使用以下任一定位器策略 executeScript()sendKeys()elementToBeClickable()

  • 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");
    
  • 浏览器快照:

美国旅游

于 2020-06-19T11:49:36.277 回答
0

我建议尝试使用 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");
     }

输出:

在此处输入图像描述

此外,您可以修改代码以根据您的要求工作。

于 2020-06-19T03:04:37.820 回答