我想在element.click()
上使用if条件,
有人可以帮帮我吗?
有两个不同的xpath,如果用户单击不存在的第一个xpath,则单击第二个元素
try{
WebElement element = driver.findElement(By.xoath("//*[@id='vehicleNum']"));
if(element.isDisplayed() && element.isEnabled()){
element.click(); // here i want if element.click(); command
}
}catch(Exception e){
WebElement element_1 = driver.findElement(By.xpath(".//*[@id='service_auto"));
if(element_1.isDisplayed() && element_1.isEnabled()){
element_1.sendKeys("admin");
}
}
答案 0 :(得分:1)
希望这会对你有所帮助:
try{
WebElement element = driver.findElement(By.xoath("//*[@id='vehicleNum']"));
if(isDisplayed(element) && isEnabled(element)){
element.click(); // here i want if element.click(); command
}
element = driver.findElement(By.xpath(".//*[@id='service_auto"));
if(isDisplayed(element) && isEnabled(element)){
element.click(); // here i want if element.click(); command
}
}
catch(Exception e){
System.out.print(e.getMessage());
}
public boolean isDispalyed(WebElement element) {
try {
return element.isDisplayed();
} catch (Exception e) {
return false;
}
}
public boolean isEnabled(WebElement element) {
try {
return element.isEnabled();
} catch (Exception e) {
return false;
}
}