直接设置value属性, 此方法主要应对输入框自动补全以及readonly属性的element,sendkeys不稳定

比如:

selenium webdriver——JS对Input执行输入


Demo:用JS输入

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


/**
* @author Hjianhui
* 12306  2016-08-04 
* 
*/
public class jsInput{

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        try{
            driver.get("https://kyfw.12306.cn/otn/#nogo");            
            JavascriptExecutor driver_js= (JavascriptExecutor) driver;
            
            //输入出发地
            driver_js.executeScript("arguments[0].value=\"广州\"",driver.findElement(By.id("fromStationText")));
        //  driver_js.executeScript("document.getElementById(\"fromStationText\").value=\"广州\"");
            
            //输入目的地
            driver_js.executeScript("arguments[0].value=\"深圳\"",driver.findElement(By.id("toStationText")));
            
            //输入出发时间
            driver_js.executeScript("arguments[0].value=\"2016-08-20\"",driver.findElement(By.id("train_date")));
        }catch (Exception e){
            e.printStackTrace();
        }
        driver.quit();    
    }
}

 

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
猜你喜欢
  • 2021-12-08
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
相关资源
相似解决方案