Java 如果未找到元素,则继续;如果找到元素,则保存它
我有一个循环,我一个接一个地打开链接。在这个循环中,我有一个if语句,它检查:Java 如果未找到元素,则继续;如果找到元素,则保存它,java,selenium,Java,Selenium,我有一个循环,我一个接一个地打开链接。在这个循环中,我有一个if语句,它检查: 如果我看到这个名字,我就复制它 若我并没有看到名称,那个么我将忽略它并继续循环 List<WebElement> demovar = driver.findElements(By.xpath("//*[@id=\"big_icon_view\"]/ul/li/p/a")); System.out.println(demovar.size()); ArrayList<String> hrefs
List<WebElement> demovar = driver.findElements(By.xpath("//*[@id=\"big_icon_view\"]/ul/li/p/a"));
System.out.println(demovar.size());
ArrayList<String> hrefs = new ArrayList<String>();
for (WebElement var : demovar) {
System.out.println(var.getText());
System.out.println(var.getAttribute("href"));
hrefs.add(var.getAttribute("href"));
}
int i = 0;
for (String href : hrefs) {
driver.navigate().to(href);
System.out.println((++i) + ": navigated to URL with href: " + href);
if(driver.findElement(By.xpath("//a[@id='name']")).isDisplayed()) {
System.out.println("I can see Name");
} else {
System.out.println("I cant see Name");
}
Thread.sleep(3000); // To check if the navigation is happening properly.
}
List demovar=driver.findElements(By.xpath(“/*[@id=\'big\u icon\u view\”]/ul/li/p/a”);
System.out.println(demovar.size());
ArrayList hrefs=新的ArrayList();
for(WebElement变量:demovar){
System.out.println(变量getText());
System.out.println(var.getAttribute(“href”);
添加(var.getAttribute(“href”);
}
int i=0;
for(字符串href:hrefs){
driver.navigate().到(href);
System.out.println((++i)+“:使用href:“+href”导航到URL;
if(driver.findelelement(By.xpath(//a[@id='name'])).isDisplayed()){
System.out.println(“我可以看到名称”);
}否则{
System.out.println(“我看不到名称”);
}
Thread.sleep(3000);//检查导航是否正常进行。
}
我不确定您在这里看到的是什么错误消息,但是如果您的代码不工作,那么很可能该元素没有显示在页面上,因此您在尝试查找它时将收到异常 您可以捕获
NoSuchElementException
来处理元素未出现在页面上的情况
for (String href : hrefs) {
driver.navigate().to(href);
System.out.println((++i) + ": navigated to URL with href: " + href);
// create isDisplayed variable
boolean isDisplayed = true;
try {
isDisplayed = driver.findElement(By.xpath("//a[@id='name']")).isDisplayed();
}
catch(NoSuchElementException) {
isDisplayed = false;
}
// do something else here with isDisplayed
if (isDisplayed) { System.out.println("I can see Name"); }
else { System.out.println("I can not see Name"); }
}
这段代码的功能与您的代码几乎相同,但是我们捕获了NoSuchElementException
,如果元素没有出现在页面上,就会抛出它
for (String href : hrefs) {
driver.navigate().to(href);
System.out.println((++i) + ": navigated to URL with href: " + href);
// create isDisplayed variable
boolean isDisplayed = true;
try {
isDisplayed = driver.findElement(By.xpath("//a[@id='name']")).isDisplayed();
}
catch(NoSuchElementException) {
isDisplayed = false;
}
// do something else here with isDisplayed
if (isDisplayed) { System.out.println("I can see Name"); }
else { System.out.println("I can not see Name"); }
}
如果这对您不起作用,请随时发布代码中的错误消息或结果,这将有助于跟踪问题。接口WebDriver中findElement(By By)的API声明如下
使用给定的方法查找第一个WebElement。此方法受执行时有效的“隐式等待”时间的影响。findElement(..)调用将返回匹配的行,或重复尝试,直到达到配置的超时。findElement不应用于查找不存在的元素,请使用findElement(By)并改为断言零长度响应。“
这意味着,如果找不到元素,它会一直尝试,直到配置超时,并抛出异常NoSuchElementException-如果找不到匹配的元素
因此,最好按以下方式处理
if(driver.findElements(By.ByXPath).size()在@Christine help之后,我为自己做了一个解决方案
for (String href : hrefs) {
driver.navigate().to(href);
boolean isPresent = driver.findElements(By.xpath("element")).size() > 0;
if (isPresent) {
String test = driver.findElement(By.xpath("element")).getText();
System.out.println(test);
} else {
System.out.println("Name not found");
}
Thread.sleep(3000); // To check if the navigation is happening properly.
}
}
}
这工作正常=)运行此操作时得到的输出是什么?如果您的失败,因为您正在查找web上不存在的元素,并且您得到了元素未找到异常。例如,假设您找到了元素,它将通过,但当它不在页面上,并且您正在查找该元素时,它将不会进入
由于元素不在页面上,您将遇到异常。有什么问题?
您可以告诉我们有什么问题。检查控制台并将异常消息(如果有)添加到您的问题中。布尔值isDisplayed=true;尝试{Thread.sleep(1500);isDisplayed=driver.findElement(By.xpath(“/*[@id=\'emailLink\']”).isDisplayed();}catch(NoSuchElementException e){isDisplayed=false;}--未找到元素=)因为打开的第一个链接没有此元素,所以,假设,如果未找到元素,则继续工作,直到找到此元素为止,通过执行-boolean isPresent=driver.findElements(by.xpath(“//h3[@id='mailttitle']),应该在4或5 linkfixed处找到它。size()>0;