我是Selenium
的新手。
我的问题是我正在尝试点击一个元素,但Selenium
正在抛出timeout exception
,即使我增加了超时值。
我是否需要使用xpath
代替id
?
HTML代码是:
void searchquotation() throws TimeoutException {
try {
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("SearchButton")));
element.click();
}
catch(TimeoutException e) {
System.out.println("Timeout occured");
}
我做错了吗?
答案 0 :(得分:0)
此处的输入类型是提交(通过查看您的HTML代码),因此我强烈建议您尝试使用Selenium的submit()函数。
答案 1 :(得分:0)
而不是By.name
,您应该使用By.id
代替By.Id("SearchButton")
。因此,请使用以下任一方法:
By.CssSelector("input#SearchButton")
By.Xpath("//input[@id='SearchButton']")
注意:语法可能有误,请根据您的编程语言进行调整
答案 2 :(得分:-1)
try below code, even timeout exception occurs, it will try 4 time to click on it. assuming locator is correct By.name("SearchButton")
public void searchquotation()
int count=0;
while(count<4)
{
try {
WebElement x = driver.findElement(By.name("SearchButton")));
WebDriverWait element=new WebDriverWait(driver,15);
element.until (ExpectedConditions.presenceOfElementLocated(By.name("SearchButton")));
x.click();
count=count+4;
}
catch(TimeoutException e) {
count=count+1;
System.out.println("Timeout occured");
continue;
}
}