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

Click() or Submit() is not working for Selenium java code

本文关键字:Selenium java 代码 适用于 Submit Click 不适用      更新时间:2023-09-26

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

Web驱动程序代码:

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...上执行click,而不是在元素本身上。

你也需要为按钮做一个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,因为它在正确构建的页面上是唯一的。