繁体   English   中英

没有可用的提交按钮时,将键发送到表单字段的问题

[英]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.

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