sendKeys()未通过Selenium和Java使用WebDriverWait插入完整值
sendKeys()未将完整字符串插入文本字段。我试图插入一个电子邮件idsendKeys()未通过Selenium和Java使用WebDriverWait插入完整值,java,selenium,selenium-webdriver,webdriver,webdriverwait,Java,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,sendKeys()未将完整字符串插入文本字段。我试图插入一个电子邮件id String name = "New Apollo33"; fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessname"))).sendKeys(name); String email = "apollo33@mailinator.com"; fluent_wait.until(ExpectedConditions.
String name = "New Apollo33";
fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessname"))).sendKeys(name);
String email = "apollo33@mailinator.com";
fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessemail"))).sendKeys(email);
它正在插入名称,但没有完全插入电子邮件id。我经常发现sendKeys()移动太快或输入的字符串不完整。
String name = "New Apollo33";
fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessname"))).sendKeys(name);
String email = "apollo33@mailinator.com";
fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessemail"))).sendKeys(email);
您需要在一个while循环中以非常小的延迟完成此操作:
while(element.text != input) {
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), input); //or whatever
Thread.sleep(100) //100ms not 100sec
}
我知道,睡眠是很不受欢迎的,然而等待也需要同样长的时间
element.clear()
也可以工作,但是使用clear()或Ctrl+a+输入,因为两者都是多余的在输入预格式化的电话号码字段时,我在自动化一个测试用例时遇到了类似的问题。以下是我为确保脚本能够在文本框中输入文本所做的操作:
- 手动检查文本框是否接受脚本尝试输入的文本长度。检查两次也无妨:)李>
- 使用sendKeys输入值并从DOM中获取元素的值
- 当DOM中文本框元素的值不等于您尝试输入的文本时,请重试
- 确保您在while循环中设置了退出条件,以便有办法退出测试
Wait<WebDriver> fluent_wait = new FluentWait<WebDriver>(driver)
.withTimeout(60, SECONDS)
.pollingEvery(2, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement emailElement= fluent_wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("businessemail"));
}
});
String emailText = "apollo33@mailinator.com";
long startTime = System.currentTimeMillis(); // This is to run the while loop for a specified amount of time and use it as an exit condition for the while loop.
//the below condition assumes that the text box sets some kind of attribute in the DOM element once the user enters the value in the text box.
while(!emailElement.getAttribute("value").equals(emailText)&&System.currentTimeMillis() - startTime) < 2000){
emailElement.clear();
emailElement.sendKeys(emailText);
}
//if the above doesn't work, add the below as a fall back:
if(!emailElement.getAttribute("value").equals(emailText)){
emailElement.clear();
for(char ch: emailText.toCharArray()){
emailElement.sendKeys(String.valueOf(ch));
try{
Thread.sleep(15); //making the thread sleep for 15 milliseconds, taking a performance hit to make the sendKeys work.
}catch(InterruptedException e){
}
}
}
Wait fluent\u Wait=新FluentWait(驱动程序)
.带超时(60秒)
.每(2秒)轮询一次
.忽略(NoSuchElementException.class);
WebElement emailElement=fluent\u wait.until(新函数(){
公共WebElement应用(WebDriver){
返回driver.findElement(By.id(“businessemail”);
}
});
字符串emailText=”apollo33@mailinator.com";
long startTime=System.currentTimeMillis();//这是在指定的时间内运行while循环,并将其用作while循环的退出条件。
//下面的条件假设一旦用户在文本框中输入值,文本框就会在DOM元素中设置某种属性。
而(!emailElement.getAttribute(“value”).equals(emailText)和&System.currentTimeMillis()-startTime)<2000){
emailElement.clear();
emailElement.sendKeys(emailText);
}
//如果上述方法不起作用,请添加以下内容作为后备措施:
如果(!emailElement.getAttribute(“值”).equals(emailText)){
emailElement.clear();
for(char ch:emailText.toCharArray()){
emailElement.sendKeys(String.valueOf(ch));
试一试{
Thread.sleep(15);//使线程睡眠15毫秒,以使sendKeys工作。
}捕捉(中断异常e){
}
}
}
只有当while循环无法将文本框中的文本设置为2秒时,才会执行Thread.sleep的后退条件。如果2秒/2000毫秒不够,您可以增加时间。thread.sleep在每个角色迭代之间的命中时间只有15毫秒。我们将其纳入我们的框架中,以涵盖在不同前端技术中开发的各种文本框。作为一个组织,这对我们来说很好,所以希望它也能对您起作用
上面的关键是不要拘泥于selenium提供的预定义实用程序,而是希望使用Java提供的DOM值和选项。希望您能很快找到解决问题的方法。
祝你好运 在尝试发送字符序列而不是调用presenceOfElementLocated()
时,请始终调用elementtobelickable()
,您可以使用以下解决方案:
fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessname"))).sendKeys("New Apollo33");
fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessemail"))).sendKeys("apollo33@mailinator.com");
我也有同样的问题。sendKeys-Keys.chord没有插入完整值(速度太快,浏览器“有时”甚至没有时间写入) 之前:(不工作100%) 写“字符串”的时候速度太快了。。有时(比如从1/5到1/10次),它在我发送到的网站上看起来不完整。这非常令人沮丧 我尝试应用的解决方案在几天内对我有效,但最终还是发生了同样的情况(1/20,但失败了)。我尝试的解决方案只是添加了一个 sendKeys-Keys.chord之后1.5seg的“线程睡眠”(可能网站需要更多的时间来编写): 之后:(没有100%起作用-我也尝试了这个可能的解决方案,但失败率较低,但仍然失败了1/20)
最终解决方案:(100%有效) 添加一个do while,它将压缩/检查sendKeys-Keys.chord是否与浏览器(WebDriver)上实际写入的内容相匹配,如果不匹配,它将再次写入(您现在可以减少Thread.sleep的延迟)
希望这个解决方案能帮助有同样问题的人^^
干杯你应该考虑把它分解成至少两个步骤,也许更多。使用流畅的等待,创建WebElement,清除文本,发送文本。您没有清除任何预先存在的文本。这不是问题所在,但将其他三个步骤合并为一个并不总是最好的主意。此外,存在只是意味着元素在DOM中,而不是它是可见的,等等。正如OP所指出的,可能重复的是,部分文本正在生成,而不是全部,并且他没有事先清除文本(你也没有)。这将潜在地保持文本的连贯性,并进入无限循环。这仍然很容易进入无限循环。您无法退出
,而。最好一次发送一个字符,字符之间有一个短暂的停顿,然后在最后断言字符串是否完整。im用于快速可读性,而不是一次迭代一个字符。不管你是否吝啬,这种方法会让用户再次行动起来。如果我们陷入了一个循环,我们等待的时间就不够长。如果OP想要一个绝对正确的方法,他们应该重写sendkeys本身。
action.sendKeys(Keys.chord("string")).perform();
Thread.sleep(1500);
WebDriverWait wait = new WebDriverWait(driver,40);
Actions action = new Actions(driver);
String TextSentenceORWordToIntroduce = "Dinosaur";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_element"))).click();
Thread.sleep(200);
action.sendKeys(Keys.chord(TextSentenceORWordToIntroduce)).perform();
Thread.sleep(500);
String comprovation = driver.findElement(By.xpath("xpath_to_element")).getAttribute("value"); // or rarely ")).getText();"
if (!comprovation.contentEquals(TextSentenceORWordToIntroduce)) { // if not then ALL WORKS FINE ;) and won't enter the Do-while
driver.findElement(By.xpath("xpath_to_element")).click();
do {
Thread.sleep(200);
action.sendKeys(Keys.HOME).perform();
Thread.sleep(200);
action.keyDown(Keys.SHIFT).perform();
Thread.sleep(200);
action.sendKeys(Keys.END).perform();
Thread.sleep(200);
action.keyUp(Keys.SHIFT).perform();
Thread.sleep(200);
action.sendKeys(Keys.chord(TextSentenceORWordToIntroduce)).perform();
Thread.sleep(500);
comprovation = driver.findElement(By.xpath("xpath_to_element")).getAttribute("value"); // or rarely ")).getText();"
} while (!comprovation.contentEquals(TextSentenceORWordToIntroduce));
}