如果不是Selenium脚本,则不执行其他条件

时间:2019-09-21 15:05:23

标签: java selenium-webdriver

我试图执行以下脚本,但是在if编写的情况下,永远不会执行else条件。我还尝试编写try catch块,并在其中编写了if条件。还是一样的问题。请帮我。下面是我尝试的代码。

仅在其他情况下

WebElement menufold=driver.findElement(By.xpath("//*[@class='anticon anticon-menu-fold trigger']"));
            WebElement menuunfold=driver.findElement(By.xpath("//*[@class='anticon anticon-menu-unfold trigger']"));


                        if(menuunfold.isDisplayed()){
                        //menufold.click();
                            Thread.sleep(5000);
                            WebElement e=driver.findElement(By.xpath("//*[@class='anticon anticon-table']"));
                            Actions a =new Actions(driver);
                            a.moveToElement(e).build().perform();
                            Thread.sleep(2000);
                            List <WebElement> orglist=driver.findElements(By.xpath("//*[@class='ant-menu-submenu-title']"));
                            a.moveToElement(orglist.get(1)).build().perform();
                            Thread.sleep(2000);
                            List <WebElement> profclick=driver.findElements(By.xpath("//a[starts-with(@href,'/profile/')]"));
                            profclick.get(0).click();
                        }

                        else if (menufold.isDisplayed()) {
                            //menuunfold.click();
                            Thread.sleep(5000);
                            driver.findElement(By.xpath("//*[@class='anticon anticon-table']")).click();
                            Thread.sleep(2000);
                            List <WebElement> orglist1=driver.findElements(By.xpath("//*[@class='ant-menu-submenu-title']"));
                            orglist1.get(1).click();
                            Thread.sleep(2000);
                            driver.findElement(By.xpath("//a[starts-with(@href,'/profile/')]")).click();


}

仅使用try catch

WebElement menufold=driver.findElement(By.xpath("//*[@class='anticon anticon-menu-fold trigger']"));
            WebElement menuunfold=driver.findElement(By.xpath("//*[@class='anticon anticon-menu-unfold trigger']"));

try {
                        if(menuunfold.isDisplayed()){
                        //menufold.click();
                            Thread.sleep(5000);
                            WebElement e=driver.findElement(By.xpath("//*[@class='anticon anticon-table']"));
                            Actions a =new Actions(driver);
                            a.moveToElement(e).build().perform();
                            Thread.sleep(2000);
                            List <WebElement> orglist=driver.findElements(By.xpath("//*[@class='ant-menu-submenu-title']"));
                            a.moveToElement(orglist.get(1)).build().perform();
                            Thread.sleep(2000);
                            List <WebElement> profclick=driver.findElements(By.xpath("//a[starts-with(@href,'/profile/')]"));
                            profclick.get(0).click();
                        }
}catch(Exception e) {
                         if (menufold.isDisplayed()) {
                            //menuunfold.click();
                            Thread.sleep(5000);
                            driver.findElement(By.xpath("//*[@class='anticon anticon-table']")).click();
                            Thread.sleep(2000);
                            List <WebElement> orglist1=driver.findElements(By.xpath("//*[@class='ant-menu-submenu-title']"));
                            orglist1.get(1).click();
                            Thread.sleep(2000);
                            driver.findElement(By.xpath("//a[starts-with(@href,'/profile/')]")).click();
                        }

每次执行代码时,都会得到此异常:

  

org.openqa.selenium.NoSuchElementException:否这样的元素:无法找到元素:{“方法”:“ xpath”,“选择器”:“ // * [@ class ='anticon anticon-menu-folding trigger' ]“}

这意味着我的else块将被忽略。请帮助我。

0 个答案:

没有答案