摘要: selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改 首先html源文件如下,设置为text 、hidden、submit View Code 在浏览器加载之后如下: 这时候email ...
selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改 首先html源文件如下,设置为text 、hidden、submit View Code 在浏览器加载之后如下: 这时候email 不能对外显示 使用selenium,代码如下 |
1 import org.openqa.selenium.Alert;2 import org.openqa.selenium.JavascriptExecutor;3 import org.openqa.selenium.By;4 import org.openqa.selenium.WebDriver;5 import org.openqa.selenium.WebElement;6 import org.openqa.selenium.chrome.ChromeDriver;7 //import org.openqa.selenium.ie.InternetExplorerDriver;8 //import org.openqa.selenium.remote.DesiredCapabilities;910 public class selenium {1112 /**13 * @param args14 * @throws InterruptedException15 */16 public static void main(String[] args) throws InterruptedException {17 // TODO Auto-generated method stub1819 String URL="E:\\2.html";20 //set web driver property21 System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");22 //create a WebDriver instance23 WebDriver driver = new ChromeDriver() ;24 driver.manage().window().maximize();2526 //load the URL27 driver.get(URL);28 //print current title29 System.out.println(driver.getTitle());30 //run JS to modify hidden element31 ((JavascriptExecutor)driver).executeScript("document.getElementById(\"em\").type ='text';");32 Thread.sleep(3000);33 //run JS and add a alert34 ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\"");3536 //wait for 3 seconds37 Thread.sleep(3000);383940 //create a alert instance41 Alert alert1=driver.switchTo().alert();42 //print alert text43 System.out.println(alert1.getText());44 //click accept button45 alert1.accept();4647 //create elements48 WebElement we=driver.findElement(By.id("fn"));49 WebElement su=driver.findElement(By.id("su"));50 WebElement em=driver.findElement(By.id("em"));51 // input something52 we.sendKeys("username test");53 Thread.sleep(3000);54 //print email tagname55 System.out.print("Email isDislayed="+em.isDisplayed()+"\n");56 Thread.sleep(3000);57 //click submit button58 su.click();59 Thread.sleep(3000);6061 Alert alert=driver.switchTo().alert();62 System.out.print( alert.getText());63 alert.accept();6465 Thread.sleep(3000);6667 //close web browser68 driver.quit();6970 }7172 } 可以通过js修改input的type value,执行js只需要export import org.openqa.selenium.JavascriptExecutor; 运行结果如下: Starting ChromeDriver (v2.9.248315) on port 30175 this is a test hello,this is a alert! Email isDislayed=true I am an alert box!! selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改 首先html源文件如下,设置为text 、hidden、submit View Code 在浏览器加载之后如下: 这时候email 不能对外显示 使用selenium,代码如下1 import org.openqa.selenium.Alert;2 import org.openqa.selenium.JavascriptExecutor;3 import org.openqa.selenium.By;4 import org.openqa.selenium.WebDriver;5 import org.openqa.selenium.WebElement;6 import org.openqa.selenium.chrome.ChromeDriver;7 //import org.openqa.selenium.ie.InternetExplorerDriver;8 //import org.openqa.selenium.remote.DesiredCapabilities;910 public class selenium {1112 /**13 * @param args14 * @throws InterruptedException15 */16 public static void main(String[] args) throws InterruptedException {17 // TODO Auto-generated method stub1819 String URL="E:\\2.html";20 //set web driver property21 System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");22 //create a WebDriver instance23 WebDriver driver = new ChromeDriver() ;24 driver.manage().window().maximize();2526 //load the URL27 driver.get(URL);28 //print current title29 System.out.println(driver.getTitle());30 //run JS to modify hidden element31 ((JavascriptExecutor)driver).executeScript("document.getElementById(\"em\").type ='text';");32 Thread.sleep(3000);33 //run JS and add a alert34 ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\"");3536 //wait for 3 seconds37 Thread.sleep(3000);383940 //create a alert instance41 Alert alert1=driver.switchTo().alert();42 //print alert text43 System.out.println(alert1.getText());44 //click accept button45 alert1.accept();4647 //create elements48 WebElement we=driver.findElement(By.id("fn"));49 WebElement su=driver.findElement(By.id("su"));50 WebElement em=driver.findElement(By.id("em"));51 // input something52 we.sendKeys("username test");53 Thread.sleep(3000);54 //print email tagname55 System.out.print("Email isDislayed="+em.isDisplayed()+"\n");56 Thread.sleep(3000);57 //click submit button58 su.click();59 Thread.sleep(3000);6061 Alert alert=driver.switchTo().alert();62 System.out.print( alert.getText());63 alert.accept();6465 Thread.sleep(3000);6667 //close web browser68 driver.quit();6970 }7172 }