一、先在allocationcommon中写个方法 // 等待页面某元素加载完成(参数:type元素定位类型、by该类型对应的定位值) public static void load(String type, String by){ WebDriverWait wait = new WebDriverWait(driver, 30); if ("xpath".equals(type)) { wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(by))); } else if("id".equals(type)) { wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(by))); } else if("name".equals(type)) { wait.until(ExpectedConditions.visibilityOfElementLocated(By.name(by))); } else if("cssSelector".equals(type)) { wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(by))); } else if("linkText".equals(type)) { wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(by))); } else { System.err.println("请输入正确的元素定位方式:xpath、id、name、cssSelector、linkText之一"); } }
二、在Element中调用此方法
public static WebElement allocationButton(WebDriver webDriver){ String xpath = "(//button[@type='button'])[4]"; MachineCommon.load("xpath",xpath); webElement = webDriver.findElement(By.xpath(xpath)); return webElement;
三、在具体的test中,调用此元素
MachineElement.allocationButton(driver).click();