java - Click() 或 Submit() 不适用于 Selenium java 代码

标签 java javascript html selenium automation

我正在尝试在搜索框中输入文本,然后单击文本应打开其下方的链接。 到目前为止,我可以在搜索框中输入文本,但无法识别输入或单击或提交。理想情况下,如果我们输入文本并单击 Enter,它应该显示搜索文本中的元素。

网络驱动程序代码:

WebDriverWait wait = new WebDriverWait(wd, 100);
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("span16"))‌​);

WebElement signupForm = wd.findElement(By.className("span16"));
signupForm.sendKeys("MTestin");

WebDriverWait wait1 = new WebDriverWait(wd, 100);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("omedia-s‌​earch"))).click();

页面 HTML:

<div class="row-fluid sort-text">
    <input id="searchbox" class="span16" type="text" placeholder="showing all result" data-provide="typeahead" name="input" />
    <i class="omedia-search"></i>
</div>

最佳答案

您正在 wait.until... 上进行单击,而不是在元素本身上进行。

您还需要为按钮执行 wd.findElement 操作,就像为搜索框执行操作一样。

此外,我认为可以安全地假设第一个 wait 调用完成后页面已完全加载,因此也无需等待找到按钮。

这是我的做法:

WebDriverWait wait = new WebDriverWait(wd, 100);
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("span16"))‌​);

WebElement signupForm = wd.findElement(By.className("span16"));
signupForm.sendKeys("MTestin");

WebElement submitButton = wd.findElement(By.className("omedia-s‌​earch"));
submitButton.click();
<小时/>

顺便说一句,您搜索的术语不是很好 - span16 类是结构化的,因此如果页面设计发生更改,它可能会出现在其他位置。相反,搜索字段的 id,因为它在正确构建的页面上保证是唯一的。

关于java - Click() 或 Submit() 不适用于 Selenium java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24735975/

相关文章:

jquery - 无法删除我的列表元素符号样式,即使它在 CSS 中设置为无

html - 透明文本但CSS中的背景不透明

javascript - 如何更新我的选择的值

java - 从 MvcResult 对象获取响应

Java 扫描器查找标签,然后使用分隔符将该标签中的内容写入文件

java - 使用简单 boolean 值时遇到问题

javascript - Angular : passing object to different view in same controller

javascript - 浏览器上传文件的问题

java - 查找以编程方式创建的 TextView?

javascript - 当我提醒数组引用时,我得到的是字符而不是单词