亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

sendKeys() 未使用網絡驅動程序通過硒和 Java 插入完整值

sendKeys() 未使用網絡驅動程序通過硒和 Java 插入完整值

白衣染霜花 2022-09-22 15:44:23
sendKeys() 未將完整字符串插入到文本字段中。我試圖插入一個電子郵件ID。String name = "New Apollo33";fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessname"))).sendKeys(name);String email = "[email protected]";fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessemail"))).sendKeys(email);它正在插入名稱,但未完全插入電子郵件 ID。
查看完整描述

3 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

我有同樣的問題。發送鍵-鍵.chord 沒有插入完整值(速度太快,瀏覽器甚至沒有時間“有時”寫入它)


之前: (100% 不起作用)


action.sendKeys(Keys.chord("string")).perform();

在編寫“字符串”時,這太快了。有時(如從1/5到1/10次)它在我發送它的網站上顯示為不完整。這非常令人沮喪。


我試圖應用的解決方案為我工作了幾天,但最終發生了同樣的事情(1/20,但它失敗了),我嘗試的解決方案只是在sendKeys-Keys.chord之后添加1.5seg的“線程睡眠”(也許網站需要更多時間來編寫它):


之后:(不能100%工作 - 我也嘗試過這個可能的解決方案,它只是失敗較少,但仍然失敗1/20)


action.sendKeys(Keys.chord("string")).perform();

Thread.sleep(1500);

最終解決方案:(100%有效)


添加一個操作時,它將進行編譯/檢查發送鍵鍵.chord是否與瀏覽器上實際寫入的內容匹配(WebDriver),如果它不匹配,它將再次寫入它(您現在可以減少線程睡眠的延遲)。


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));

}

希望這個解決方案可以幫助有同樣問題的人^^


查看完整回答
反對 回復 2022-09-22
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

在輸入預先格式化的電話號碼字段時自動執行一個測試用例時,我遇到了類似的問題。以下是我為確保腳本能夠在文本框中輸入文本所做的工作:

  • 手動檢查文本框是否接受腳本嘗試輸入的文本長度。檢查兩次:)永遠不會有什么壞處。

  • 使用發送鍵輸入值,并從 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 = "[email protected]";

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){

        }

    }

}

僅當 while 循環無法將文本框中的文本設置為 2 秒時,才會執行 Thread.sleep 的回退條件。如果 2 秒/2000 毫秒不夠,您可以增加時間。線程睡眠在每次字符迭代之間命中的時間僅為 15 毫秒。我們將其合并到我們的框架中,以涵蓋以不同前端技術開發的各種文本框。這對我們作為一個組織來說很好,所以希望它也能為你工作。


上面的關鍵是不要陷入硒提供的預定義實用程序,而是要使用Java提供的DOM值和選項。希望您能夠盡快找到解決問題的方法。祝你好運!


查看完整回答
反對 回復 2022-09-22
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

作為嘗試發送字符序列而不是調用始終調用元素 ToBeCable() 時的拇指規則,您可以使用以下解決方案:presenceOfElementLocated()

fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessname"))).sendKeys("New Apollo33");
fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessemail"))).sendKeys("[email protected]");


查看完整回答
反對 回復 2022-09-22
  • 3 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號