Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用if/else处理警报_Java_Selenium_Webdriver - Fatal编程技术网

在Java中使用if/else处理警报

在Java中使用if/else处理警报,java,selenium,webdriver,Java,Selenium,Webdriver,如何使用if/else命令处理警报?如果出现警报,则执行接受/解除操作,如果没有,则继续操作。我尝试使用下面的代码,但是(r==true)上的错误表示类型不兼容 bool r = driver.findElement(By.xpath("//div[contains(text(),'javax.baja.sys.ActionInvokeException')]")); if (r = true) { driver.switchTo().alert().accept(); } else {

如何使用if/else命令处理警报?如果出现警报,则执行接受/解除操作,如果没有,则继续操作。我尝试使用下面的代码,但是(r==true)上的错误表示类型不兼容

bool r = driver.findElement(By.xpath("//div[contains(text(),'javax.baja.sys.ActionInvokeException')]"));
if (r = true) {
    driver.switchTo().alert().accept();
} else {
    Actions click2 = new Actions(driver);
    WebElement dclick2 = driver.findElement(By.xpath(".//span[text()='Bluemix_As_Device']"));
    click2.moveToElement(dclick2).doubleClick().build().perform();
}

由于r是布尔类型,因此不需要编写if(r==true)或if(r==false),您可以直接编写if(r),java将理解代码。

不兼容的类型是因为

driver.findElement 
将返回一个类型,而不是
布尔值(这是java)。您可能希望将代码更改为:

try {
    WebElement r = driver.findElement(By.xpath("//div[contains(text(),'javax.baja.sys.ActionInvokeException')]"));
    driver.switchTo().alert().accept(); // this would be executed only if above element is found
} catch (NoSuchElementException ex) {
    // since the element was not found, I 'm still doing some stuff
    Actions click2 = new Actions(driver);
    WebElement dclick2 = driver.findElement(By.xpath(".//span[text()='Bluemix_As_Device']"));
    click2.moveToElement(dclick2).doubleClick().build().perform();
}

根据您的建议,我尝试了bool r=driver.findelelement(By.xpath(“//div[contains(text(),'javax.baja.sys.actioninvokeeexception'))));if(r){但仍然面临相同的问题。如果(r=true)不需要写,您应该只写if(r),因为r是布尔类型。或者您也可以写if(r==true)
=
是的。
=
是的。另外,
bool
在Java中不是一个关键字。这段代码真的编译了吗?@nbrooks这似乎是一个打字错误,文本上写着
r==true
,但代码读起来是
r=true
,错误是针对不兼容的类型。不编辑该部分,依靠OP来更新打字。@nullpointer无论问题中的描述如何,代码都是不正确的。我提到的两个错误都是编译器会报告的。要么我们没有查看OP的实际代码,要么OP没有尝试编译此代码或与我们共享错误。无论哪种方式,此问题都有一些问题。此问题有一些问题sues.同意。感谢澄清,此代码在警报出现时工作正常,但在警报未出现时失败。在我的应用程序中,警报pop在同一操作中不一致显示。因此,我希望处理这样一种方式,即当警报出现时,接受/取消警报,如果警报未出现在然后跳到下一行代码,不会失败。我不确定如何处理这个命令可能是if/else?@Abhi不是真的if/else,如果您可能遇到任何其他异常抛出,您可以将
异常捕获为泛型。
driver.findElements will check the existence of the object and will return 1 if exist else zero.
So in your case though the alert exist or not, it will handle and based on size it will execute next step. Hope this helps in your case.

int r= driver.findElements(By.xpath("//div[contains(text(),'javax.baja.sys.ActionInvokeException')]")).size();
if(r!=0){
    driver.switchTo().alert().accept();
} else {
    Actions click2 = new Actions(driver);
    WebElement dclick2 = driver.findElement(By.xpath(".//span[text()='Bluemix_As_Device']"));
    click2.moveToElement(dclick2).doubleClick().build().perform();
}