Java 无法通过“检查”检查WebElement是否存在;isDisplyed";关于seleniumwebdriver

Java 无法通过“检查”检查WebElement是否存在;isDisplyed";关于seleniumwebdriver,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在尝试使用(By.id)locator.getatribute(“value”)从UI获取值 问题: 但是,当找不到xpath(UI上不存在web元素)时,我需要将值设置为“00:00”,但得到的错误是“没有这样的元素” 我需要添加一个检查,以查找UI上是否存在webelement,如果为true,则使用getattribute(“value”)获取值,否则返回值为“00:00” 但是当使用If条件时,“(WebelementforUIvalue.isEmpty()”返回'00:00',尽管

我正在尝试使用(By.id)locator.getatribute(“value”)从UI获取值

问题:

但是,当找不到xpath(UI上不存在web元素)时,我需要将值设置为“00:00”,但得到的错误是“没有这样的元素”

我需要添加一个检查,以查找UI上是否存在webelement,如果为true,则使用getattribute(“value”)获取值,否则返回值为“00:00”

但是当使用If条件时,“(WebelementforUIvalue.isEmpty()”返回'00:00',尽管Web元素在UI上存在。(当UI上找不到/不存在元素时,我需要取00:00)

请改为这样做(Java代码):

我已经给出了一个20秒的显式超时。因此,selenium将在20秒内尝试检测元素的存在。如果它没有找到,那么它将超时并将分配的“UIValue”发送到“00.00”,或者如果它找到了元素,它将把“value”属性的内容分配给“UIValue”。

尝试这样做(Java代码):

我已经给出了一个20秒的显式超时。因此,selenium将在20秒内尝试检测元素的存在。如果它没有找到,那么它将超时并将分配的“UIValue”发送到“00.00”,或者如果它找到了元素,它将把“value”属性的内容分配给“UIValue”。

尝试这样做(Java代码):

我已经给出了一个20秒的显式超时。因此,selenium将在20秒内尝试检测元素的存在。如果它没有找到,那么它将超时并将分配的“UIValue”发送到“00.00”,或者如果它找到了元素,它将把“value”属性的内容分配给“UIValue”。

尝试这样做(Java代码):


我已经给出了20秒的显式超时。因此,selenium将在20秒内尝试检测元素的存在。如果没有找到,它将超时并将分配的“UIValue”发送到“00.00”,否则如果找到元素,它将把“value”属性的内容分配给“UIValue”.

就我个人而言,我会在这种特殊情况下使用try/catch块

try {
    String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
    UIValue = WebelementforUIvalue;
    } 
    catch (NoSuchElementException) {
        UIValue ="00.00";
    }

就我个人而言,我会为这个特殊的案例使用try/catch块

try {
    String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
    UIValue = WebelementforUIvalue;
    } 
    catch (NoSuchElementException) {
        UIValue ="00.00";
    }

就我个人而言,我会为这个特殊的案例使用try/catch块

try {
    String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
    UIValue = WebelementforUIvalue;
    } 
    catch (NoSuchElementException) {
        UIValue ="00.00";
    }

就我个人而言,我会为这个特殊的案例使用try/catch块

try {
    String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
    UIValue = WebelementforUIvalue;
    } 
    catch (NoSuchElementException) {
        UIValue ="00.00";
    }

你可以这样试试

String WebelementforUIvalue = null;
try{
    WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
   } catch(NoSuchElementException nsee) {
           WebelementforUIvalue = "0.0";
   }
System.out.println(WebelementforUIvalue);

你可以这样试试

String WebelementforUIvalue = null;
try{
    WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
   } catch(NoSuchElementException nsee) {
           WebelementforUIvalue = "0.0";
   }
System.out.println(WebelementforUIvalue);

你可以这样试试

String WebelementforUIvalue = null;
try{
    WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
   } catch(NoSuchElementException nsee) {
           WebelementforUIvalue = "0.0";
   }
System.out.println(WebelementforUIvalue);

你可以这样试试

String WebelementforUIvalue = null;
try{
    WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
   } catch(NoSuchElementException nsee) {
           WebelementforUIvalue = "0.0";
   }
System.out.println(WebelementforUIvalue);

您可以使用“findElements”(末尾的“observer’s”)以更好的方式处理此问题。 findElements返回与定位条件匹配的元素列表

List<WebElements> list=driver.findElements(By.id("Your criteria"))
Integer intNoValues=list.size();
if(intNoValues=0)
{  
  UIValue = "00.00";
}
else
{
 UIValue = WebelementforUIvalue;
}
List List=driver.findElements(By.id(“您的标准”))
整数intNoValues=list.size();
如果(intNoValues=0)
{  
UIValue=“00.00”;
}
其他的
{
UIValue=WebelementforUIvalue;
}
如果您想了解findElements的详细说明,可以观看以下视频

您可以使用“findElements”(末尾的“observer’s”)以更好的方式处理此问题。 findElements返回与定位条件匹配的元素列表

List<WebElements> list=driver.findElements(By.id("Your criteria"))
Integer intNoValues=list.size();
if(intNoValues=0)
{  
  UIValue = "00.00";
}
else
{
 UIValue = WebelementforUIvalue;
}
List List=driver.findElements(By.id(“您的标准”))
整数intNoValues=list.size();
如果(intNoValues=0)
{  
UIValue=“00.00”;
}
其他的
{
UIValue=WebelementforUIvalue;
}
如果您想了解findElements的详细说明,可以观看以下视频

您可以使用“findElements”(末尾的“observer’s”)以更好的方式处理此问题。 findElements返回与定位条件匹配的元素列表

List<WebElements> list=driver.findElements(By.id("Your criteria"))
Integer intNoValues=list.size();
if(intNoValues=0)
{  
  UIValue = "00.00";
}
else
{
 UIValue = WebelementforUIvalue;
}
List List=driver.findElements(By.id(“您的标准”))
整数intNoValues=list.size();
如果(intNoValues=0)
{  
UIValue=“00.00”;
}
其他的
{
UIValue=WebelementforUIvalue;
}
如果您想了解findElements的详细说明,可以观看以下视频

您可以使用“findElements”(末尾的“observer’s”)以更好的方式处理此问题。 findElements返回与定位条件匹配的元素列表

List<WebElements> list=driver.findElements(By.id("Your criteria"))
Integer intNoValues=list.size();
if(intNoValues=0)
{  
  UIValue = "00.00";
}
else
{
 UIValue = WebelementforUIvalue;
}
List List=driver.findElements(By.id(“您的标准”))
整数intNoValues=list.size();
如果(intNoValues=0)
{  
UIValue=“00.00”;
}
其他的
{
UIValue=WebelementforUIvalue;
}
如果您想了解findElements的详细说明,可以观看以下视频

这方面的典型解决方法是使用定位器构建元素列表。如果列表长度=1,则取其值。如果长度=0,则未找到元素,不会引发异常,而可以返回自己的值。或者,可以使用try/catch块来处理引发的NoTouchElement异常。这方面的典型解决方法是使用定位器构建元素列表。如果列表长度=1,则取其值。如果长度=0,则未找到元素,不会引发异常,而可以返回自己的值。或者可以使用try/catch块来处理引发的NoTouchElement异常。典型的解决方法是使用loca构建元素列表tor。如果列表长度=1,则取其值。如果长度=0,则未找到元素,不会引发异常,而可以返回自己的值。或者,可以使用try/catch块来处理引发的NoTouchElement异常。典型的解决方法是使用定位器构建元素列表。如果列表长度=1,则取its值。如果长度=0,则找不到该元素,不会引发任何异常,您可以返回自己的值。或者您可以使用try/catch块来处理引发的NoTouchElement异常。如果找不到该元素,它将引发超时异常,而不是将该元素设置为空值。@MarkRowlands:My bad。您现在已经修复了它使用try-catch。谢谢。如果找不到元素,它将抛出超时异常,而不是将元素设置为空值。@MarkRowlands:我的错。现在使用try-catch修复了它。谢谢。如果找不到元素,它将抛出超时异常,而不是设置元素