[英]Problem with sending keys to a form field when there is no submit button available
我正在為一個學校項目研究Selenium WebDriver。 我當前正在創建一個Maven Web應用程序(帶有jsp / servlet),該應用程序通過Web刮取Tripadvisor數據,將其放入數據庫中,然后根據用戶過去的行為對數據進行排序。
我的問題開始於必須將我的密鑰提交給Tripadvisor搜索欄。 沒有提交按鈕,因此我必須使用org.openqa.selenium.Keys導入。 這是我嘗試的代碼:
driver.findElement(By.xpath("//span[contains(@class, 'brand-trip-search-geopill-TripSearchGeoPill__icon--jEoJX')]")).click();
String keyword = request.getParameter("<parameter-inserted-by-user>");
//insert text inside search form
WebElement insert_element = driver.findElement(By.xpath("//input[@class='input-text-input-ManagedTextInput__managedInput--106PS']"));
insert_element.sendKeys(keyword+Keys.ENTER);
出現的問題是,當我運行測試時,在搜索表單中插入了文本,但是當Keys.ENTER發生時,搜索未提交,並且注冊的方式與我實際寫過的一樣:
insert_element.sendKeys(Keys.ENTER);
我一直潛伏在stackoverflow尋找解決方案,並且嘗試了以下替代方法:
insert_element.sendKeys(keyword + "\n");
無濟於事。 它僅注冊“ Enter”命令,因此為我提供了“附近”位置的搜索。
我還看到我可以使用javascript,但是對於諸如提交搜索請求之類的簡單任務而言,它看起來很繁重。
目前,我正在使用Chromedriver v.2.44和Selenium v.3.141.59
有人能幫我嗎? 預先感謝您的寶貴時間。
您遇到了計時問題。 Selenium的輸入速度非常快,然后按Enter鍵。 手動執行操作,您將看到根據鍵入的內容鍵入獲取結果之間的時間稍有延遲。
我有證明上述內容的示例代碼,但是請您自己找出來。 上面的注釋和您的代碼應該足夠了。
---編輯---在OP知道之后添加示例代碼
driver.findElement(By.xpath("//span[contains(@class, 'brand-trip-search-geopill-TripSearchGeoPill__icon--jEoJX')]")).click();
String keyword = request.getParameter("<parameter-inserted-by-user>");
//insert text inside search form
WebElement insert_element = driver.findElement(By.xpath("//input[@class='input-text-input-ManagedTextInput__managedInput--106PS']"));
insert_element.sendKeys(keyword);
Thread.sleep(1000); // <-- Not ideal but for a permanent solution, but illustrates this is timing related.
insert_element.sendKeys(Keys.ENTER);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.