簡體   English   中英

在Selenium腳本中按Enter鍵

[英]Press Enter key in Selenium script

我正在使用Selenium Server(v2.21)和Selenium Java Client(v.2.21.0)來自動化每個條目后需要按Enter鍵的Web表單,因為字段是根據輸入的值公開的。 所以基於這里的解決方案,我一直在嘗試不同的方式在表單中輸入字符串並按Enter鍵 - 這是我嘗試過的:

// type field value
selenium.type("program", "MIC HOMEOWNERS");

// ** not working: selenium.keyPress("program", "\\13");
// ** not working: selenium.select("program", "Program");
// ** not working: selenium.keyPressNative(Keys.ENTER.toString());
// ** not working: selenium.keyDown("program", "13");

看起來這是最合乎邏輯的解決方案( selenium.keyPressNative(Keys.ENTER) ),但是如果不添加.toString ,編譯器會拋出錯誤,因為keyPressNative需要一個String。

實際的表單代碼:

<label  >Program</label> 
    <input id="program" name="program1" class="readonly-bg" readonly="readonly" type="text" value="MIC HOMEOWNERS" size="12"/>
    <input id="program" name="program" type="hidden" value="601"/>
        <script type="text/javascript">  
            Spring.addDecoration(new Spring.ElementDecoration({  
                elementId : "program",  
                widgetType : "dijit.form.ValidationTextBox",  
                widgetAttrs : { 
                    trim:true ,
                    required : true
                }}));  
        </script>
<br>

如何模擬按Enter鍵?

我使用以下代碼單擊Escape Enter

try {
    Thread.sleep(700);
} catch (InterruptedException e) {
    selenium.keyPressNative("27"); // Escape
    selenium.keyPressNative("10"); // Enter 
}   

我們需要暫停selenium直到上一個命令成功執行。 所以我使用sleep()方法。 對於我的測試用例,我需要暫停700 MillSec。 根據您的要求,您需要更改值。

在WebDriver中有一個類鍵。 使用這個我們可以發送ENTER鍵。 像這樣

driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);

對於Selenium RC:

selenium.keyPress("elementid", "\\13"); // Note the double backslash

你試過這個嗎?

selenium.keyPressNative("\n");

嘗試這個。 我正在測試的應用程序要求用戶執行以下步驟以將值輸入表格單元格。 (單擊單元格,按回車鍵,輸入一個數字,再次按回車鍵完成)它適用於我的腳本

selenium.click("id_locator");

this.command_waitInSeconds(1);

selenium.keyPress("id_locator", "\\13");

this.command_waitInSeconds(3);

selenium.type("name=value", "10000");

selenium.focus("name=value");

selenium.keyPress("name=value", "\\13");

如果您將selenese編寫為HTML(或使用IDE),則可以在變量${KEY_ENTER}使用ENTER鍵。

以下是sendKeys的示例:

sendKeys | id=search | ${KEY_ENTER}

您可以在此處查看所有可用密鑰的列表: http//blog.reallysimplethoughts.com/2013/09/25/using-special-keys-in-selenium-ide-part-2/

你試過這個嗎? (這是紅寶石)

$driver.find_element(:id, "program").send_keys [:return]

嘗試使用XPATH搜索Element,然后使用以下代碼。 它對我有用。

 driver.findElement(By.xpath(".//*[@id='txtFilterContentUnit']")).sendKeys(Keys.ENTER);
webElem=driver.findElement(By.xpath(""));
webElem.sendKeys(Keys.TAB);
webElem.sendKeys(Keys.ENTER);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM