英文:
How to retry some methods several seconds, and when timeout throw the method's exception rather than TimeoutException in selenium?
问题
我想要创建一个选择元素来选择一个选项。但是当页面加载时,选项可能不存在。因为选项是从数据库、API或具有响应时间的其他地方加载的。
目前,我使用了Selenium的wait.until
方法与布尔条件一起使用,以创建自定义的等待方法。
我的解决方法:
.
.
.
String exceptionMessage;
public void myCode() throws Exception {
exceptionMessage = "";
Integer sec = 10;
WebDriverWait wait = new WebDriverWait(shared.getBrowser(), Duration.ofSeconds(sec));
try {
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
try {
.
.
.
CommonMethods.selectOption(element, optionText);
return true;
} catch (Exception e) {
exceptionMessage = e.getMessage();
return false;
}
}
});
} catch (TimeoutException e) {
throw new Exception("Timeout: [" + sec + "] seconds, Exception message: [" + exceptionMessage + "]");
}
}
如果超时,结果将为:
java.lang.Exception: Timeout: [10] seconds, Exception message: [element not visible: Element is not currently visible and may not be manipulated
是否有Selenium方法可以解决这个问题?或者有更好的方法吗?
英文:
I want to make a select element to select an option. But the option may not exist when the page loaded. Because the options is loading from the database, API, or something that has response time.
Currently, I use selenium wait.until with boolean condition. To make a custom wait method.
My workaround:
.
.
.
String exceptionMessage;
public void myCode() throws Exception {
exceptionMessage = "";
Integer sec = 10;
WebDriverWait wait = new WebDriverWait(shared.getBrowser(), Duration.ofSeconds(sec));
try {
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
try {
.
.
.
CommonMethods.selectOption(element, optionText);
return true;
} catch (Exception e) {
exceptionMessage = e.getMessage();
return false;
}
}
});
} catch (TimeoutException e) {
throw new Exception("Timeout: [" + sec + "] seconds, Exception message: [" + exceptionMessage + "]");
}
}
If timeout the result will be:
java.lang.Exception: Timeout: [10] seconds, Exception message: [element not visible: Element is not currently visible and may not be manipulated
Is there any selenium method for it? Or a better way?
答案1
得分: 0
等待直到定位到元素的唯一XPath,因为可能会有一些隐藏的元素。
如果上面的等待不起作用,您也可以尝试以下方法:
new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible("您的元素路径"));
请注意,您提供的代码中有一些错别字和语法错误,已在翻译中进行了修正。
英文:
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(" xpath']")));
Use unique Xpath to find the elements as there will some hidden elements.
If the above wait doesn't work, you can try the below as well:
new WebDriverWait(Driver,TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible("Yyour element path");
答案2
得分: 0
我认为你不需要那么多东西。
我的C#比我的Java好,但这是我会使用的结构。我在IntelliJ中运行了它,似乎可以工作,但我没有你的操作和网站来测试它。
我刚刚加入了一个明显会失败的元素。
取消注释后:
dr.findElement(By.tagName("I will fail you!")).click();
它会返回你的超时错误捕获行。我得到:
java.lang.Exception: Timeout: [1] seconds, Exception message: [no such
element: Unable to locate element: {"method":"css
selector","selector":"I will fail you!"} (Session info:
chrome=84.0.4147.105)
如果将其注释掉 - 它不会在异常消息中返回任何内容(因此返回true,因此返回通过)。
英文:
I don't think you need as much as you've got in there.
My c# is better than my java, but this is the sort of structure i would use.
I've run it in intellij and it seems to work - but i don't have your actions and site to test it.
@Test
public void myCode() throws Exception {
exceptionMessage = "";
Integer sec = 10;
WebDriverWait wait = new WebDriverWait(driver, sec);
try {
wait.until((WebDriver dr) ->
{
try {
//Your actions go here
//this one will always fail - comment it out to make it pass
dr.findElement(By.tagName("I will fail you!")).click();
return true;
} catch (Exception e) {
exceptionMessage = e.getMessage();
return false;
}
});
} catch (TimeoutException e) {
throw new Exception("Timeout: [" + sec + "] seconds, Exception message: [" + exceptionMessage + "]");
}
System.out.println("The exception message is.... " + exceptionMessage);
}
I've just thrown in an obvious failing element.
With this uncommented:
dr.findElement(By.tagName("I will fail you!")).click();
It returns your timeout error catch line. I get:
> java.lang.Exception: Timeout: 1 seconds, Exception message: [no such
> element: Unable to locate element: {"method":"css
> selector","selector":"I\ will\ fail\ you!"} (Session info:
> chrome=84.0.4147.105)
With it commented out - it returns nothing in the exception message (therefore returned true, therefore returned pass):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论