简体   繁体   English

使用Selenium WebDriver进行表单初始化

[英]Form Initialize using selenium webdriver

I use the following code using eclipse to initialize a form 我使用以下代码使用eclipse初始化表单

WebElement e1 = KD.findElement(By.name("name"));
e1.sendKeys("Srajan ");

But in the form the first character 'S' does not get displayed. 但是不会以第一个字符'S'的形式显示。 It starts from 'r' only. 它仅从'r'开始。 Is the issue be in the coding or the website? 问题出在编码还是网站上?

Looking at the test code and without knowing about the page source, I think it is a character limit issue. 查看测试代码并且不了解页面源代码,我认为这是字符限制问题。 Check if that field has a character limit and trimming the initial S 检查该字段是否有字符数限制并修整开头的S

Also, try using clear() before sending new ones if not the character limit issue 另外,如果不是字符限制问题,请尝试在发送新字符之前尝试使用clear()

WebElement e1 = KD.findElement(By.name("name"));
e1.clear();
e1.sendKeys("Srajan");

One more way to solve this by putting a sleep duration before typing, I fixed this in my project like: 通过在输入之前放置睡眠时间来解决此问题的另一种方法,我在项目中修复了该问题,例如:

WebElement e1 = KD.findElement(By.name("name"));
Thread.sleep(1000);
e1.sendKeys("Srajan");
Thread.sleep(500);

This should work, you may not need Thread.sleep(500); 这应该可以工作,您可能不需要Thread.sleep(500);

Yeah it happens sometimes , because the element is not loaded yet. 是的,有时会发生这种情况,因为尚未加载该元素。 You can try to use wait for that element 您可以尝试使用等待该元素

WebDriverWait wait = new WebDriverWait(driver, 20); 
      wait.until(ExpectedConditions.presenceOfElementLocated(By.name("name")));

You can also try Thread.sleep(500) but its not recommended. 您也可以尝试Thread.sleep(500),但不建议这样做。 And its also a good practice to use clear() method before sending some input. 在发送一些输入之前使用clear()方法也是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM