Webdriver利用JS给只读属性的文本框赋值

来源:互联网 发布:windows10自带优化 编辑:程序博客网 时间:2024/05/13 12:49

给只读文本框赋值,这里列举了两种方式。一种是去掉文本框的只读属性,另一种是添加value属性,直接赋值。

只读文本框截图


package com.test;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;public class ReadOnly {WebDriver driver = null;@BeforeTestpublic void beforeTest() {System.setProperty("webdriver.chrome.driver", "./plugin/driver/chromedriver.exe");driver = new ChromeDriver();}@Testpublic void readonly_input() throws InterruptedException {driver.get("file:///D:/location/locator.html");//方法一/*String remove_readonly_js = "$('#readonly').removeAttr('readonly')";if(driver instanceof JavascriptExecutor){((JavascriptExecutor)driver).executeScript(remove_readonly_js);}driver.findElement(By.id("readonly")).sendKeys("Bingo");*///方法二/*JavascriptExecutor js = null;if(driver instanceof JavascriptExecutor){js = (JavascriptExecutor)driver;}js.executeScript("$('#readonly').removeAttr('readonly')");driver.findElement(By.id("readonly")).sendKeys("Method 2");*///方法三String set_value_readonly_js = "$('#readonly').attr('value', 'Method3')";if(driver instanceof JavascriptExecutor){((JavascriptExecutor)driver).executeScript(set_value_readonly_js);}Thread.sleep(5000);}@AfterTestpublic void afterTest() {if(driver != null)driver.close();}}



0 0