How to type some text in hidden field in Selenium WebDriver using Java

前端 未结 2 988
温柔的废话
温柔的废话 2020-11-30 07:49

I am using WebDriver with Java for test automation. I have the following HTML code for input field which is hidden:



        
相关标签:
2条回答
  • 2020-11-30 08:16

    First of all you have to change the value of type attribute as text from hidden. The following code using javascript would work for that:

    jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");
    

    Now, you are able to type on that text by using WebDriver. So, the overall code for typing in a hidden field with WebDriver using Java and Javascript as follows:

    WebDriver driver = new FirefoxDriver();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");
    driver.findElement(By.xpath("//input[@name='body']")).clear();
    driver.findElement(By.xpath("//input[@name='body']")).sendKeys("Ripon: body text");
    
    0 讨论(0)
  • 2020-11-30 08:28
    WebDriver driver=new FirefoxDriver();
    driver.get("http://localhost/login.do");
    driver.manage().window().maximize();
    RemoteWebDriver r=(RemoteWebDriver) driver;
    String s1="document.getElementById('username').value='admin'";
    r.executeScript(s1);
    
    0 讨论(0)
提交回复
热议问题