Java 有没有其他方法(不是 sendKeys)使用 selenium webdriver 将文本输入到文本框中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26955263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any other way(not sendKeys) to enter text into textbox using selenium webdriver?
提问by Vasantha
Is there any other way(not sendKeys) to enter text into textbox using selenium webdriver ?
有没有其他方法(不是 sendKeys)使用 selenium webdriver 在文本框中输入文本?
回答by Anirudh
Any other way is only to use native Javascript action to enter the value in the text box:
任何其他方式都只能使用原生 Javascript 操作在文本框中输入值:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById("textbox_id").value='new value';);
回答by Virender Singh
@Vasntha, try mine code its tested one
@Vasntha,试试我的代码
driver.get("http://www.qajudge.com/");
WebElement cssValue= driver.findElement(By.xpath(".//*[@id='s']"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('s').value='Virender Testing
sending'");
回答by jagannath
WebElement name=driver.findElement(By.name("username"));
((JavascriptExecutor)driver).executeAsyncScript("arguments[0].value='admin'",name);
回答by ragesh-ragav 1993
Try below one, which I tried in Gmail and it works
尝试以下一个,我在 Gmail 中尝试过,它有效
System.setProperty("webdriver.chrome.driver", "you .exe path here");
WebDriver driver = new ChromeDriver();
driver.get("https://accounts.google.com/");
driver.manage().window().maximize();
Thread.sleep(1000);
//Next button element
WebElement nextBtn = driver.findElement(By.id("identifierNext"));
JavascriptExecutor js = (JavascriptExecutor)driver;
//code to enter value in the email textbox
js.executeScript("document.getElementById('identifierId').value='testemail'");
//code to click on next button
js.executeScript("arguments[0].click();", nextBtn);