我是
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");
}
我做错了什么吗?
这里的输入类型是submit(通过查看你的HTML代码),所以我强烈建议尝试Selenium的submit()函数。
您应该使用
By.name
,而不是 By.id
。因此,请使用以下任一方法:
By.Id("SearchButton")
By.CssSelector("input#SearchButton")
By.Xpath("//input[@id='SearchButton']")
注意:语法可能有错误,请根据您的编程语言进行调整
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;
}
}