嗨,任何人都可以解决这个问题。当我编写一个自动化的代码时,有时候元素没有被识别出来,有时即使它们存在也找不到它们,这意味着即使id存在,它也会说找不到元素的错误。所以我试图创建一个方法,我将传递所有dom对象,我发现像Ex:
public static void Click(WebDriver driver, String name,Sting linktext,Sting id,Sting Xpath,String css)
{
driver.findElement(new ByAll(By.name(name),
By.linkText(linktext),
By.id(id),
By.xpath(xpath),
By.cssSelector(css))).click();
}
我会传递我在源页面中找到的有价值的东西,有时它会有oly id或者它会有oly链接文本Ex :(当我在其他类中导入此方法时)
Click(Webdriver driver, "username",null,"","//[fas].user");
这是传递参数的正确方法。我可以通过null和"" (空白)。请帮助我成为一个简单有效的框架。
答案 0 :(得分:0)
you can use this 2 methods
public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(selector));
return findElement(driver, selector);
}
public static WebElement findElementSafe(WebDriver driver, By selector, long timeOutInSeconds) {
try {
return findElement(driver, selector, timeOutInSeconds);
} catch (TimeoutException e) {
return null;
}
}
public static void waitForElementToAppear(WebDriver driver, By selector, long timeOutInSeconds, String timeOutMessage) {
try {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
} catch (TimeoutException e) {
throw new IllegalStateException(timeOutMessage);
}
}
----------------
public static void click(WebDriver driver , By ... selector ){
for (By byPath : selector) {
WebElement element = findElementSafe(driver, byPath, 1);
if(element != null){
element.click();
}
}
}