Java Selenium Webdriver:在文本字段中输入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19137109/
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
Selenium Webdriver: Entering text into text field
提问by Adnan Ghaffar
When I enter text into the text field it gets removed.
当我在文本字段中输入文本时,它会被删除。
Here is the code:
这是代码:
String barcode="0000000047166";
WebElement element_enter = _driver.findElement(By.xpath("//*[@id='div-barcode']"));
element_enter.findElement(By.xpath("//html/body/div[1]/div[3]/div[1]/form/div/div/input")).sendKeys("barcode");
采纳答案by Eshan Liyanagama
Agree with Subir Kumar Sao and Faiz.
同意 Subir Kumar Sao 和 Faiz 的观点。
element_enter.findElement(By.xpath("//html/body/div[1]/div[3]/div[1]/form/div/div/input")).sendKeys(barcode);
回答by CODEBLACK
I had a case where I was entering text into a field after which the text would be removed automatically. Turned out it was due to some site functionality where you had to press the enter key after entering the text into the field. So, after sending your barcode text with sendKeys method, send 'enter' directly after it. Note that you will have to import the selenium Keys class. See my code below.
我有一个案例,我在一个字段中输入文本,之后文本将被自动删除。原来这是由于某些站点功能,您必须在将文本输入字段后按 Enter 键。因此,在使用 sendKeys 方法发送条码文本后,直接在其后发送“输入”。请注意,您必须导入 selenium Keys 类。请参阅下面的我的代码。
import org.openqa.selenium.Keys;
String barcode="0000000047166";
WebElement element_enter = driver.findElement(By.xpath("//*[@id='div-barcode']"));
element_enter.findElement(By.xpath("your xpath")).sendKeys(barcode);
element_enter.sendKeys(Keys.RETURN); // this will result in the return key being pressed upon the text field
I hope it helps..
我希望它有帮助..
回答by user3364839
Use this code.
使用此代码。
driver.FindElement(By.XPath(".//[@id='header']/div/div[3]/div/form/input[1]")).SendKeys("25025");
回答by Kumar
It might be the JavaScript check for some valid condition.
Two things you can perform a/c to your requirements:
它可能是 JavaScript 检查某些有效条件。
您可以根据您的要求执行空调的两件事:
- either check for the valid string-input in the text-box.
- or set a loop against that text box to enter the value until you post the form/request.
- 要么检查文本框中的有效字符串输入。
- 或针对该文本框设置循环以输入值,直到您发布表单/请求。
String barcode="0000000047166";
WebElement strLocator = driver.findElement(By.xpath("//*[@id='div-barcode']"));
strLocator.sendKeys(barcode);