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
Java 如果条件未按中的预期工作 for(int row=1;row_Java_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Java 如果条件未按中的预期工作 for(int row=1;row

Java 如果条件未按中的预期工作 for(int row=1;row,java,selenium,selenium-webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,您需要使用如下所示的try catch块- for(int row=1; row <= 6; row++) { WebDriver driver=new ChromeDriver(); driver.manage().deleteAllCookies(); driver.get("http:/path/"); driver.manage().window().maximize(); String username = s.getCell(2,row)

您需要使用如下所示的try catch块-

for(int row=1; row <= 6; row++)
{
    WebDriver driver=new ChromeDriver();
    driver.manage().deleteAllCookies();
    driver.get("http:/path/");
    driver.manage().window().maximize();
    String username = s.getCell(2,row).getContents();
    System.out.println("Username: "+username);
    driver.findElement(By.id("j_username_leftAside")).sendKeys(username);
    String password= s.getCell(3,row).getContents();
    System.out.println("Password: "+password);
    driver.findElement(By.id("j_password_leftAside")).sendKeys(password);
    driver.findElement(By.xpath("/html[@class=' js opacity generatedcontent pointerevents']/body[@class='page-homepage pageType-ContentPage template-pages-layout-RexelHomePageLayout pageLabel-homepage language-de ']/div[@id='page']/div[@id='content']/div[@id='content']/div[@class='content-top-inner']/div[@id='content-inner']/div[@class='mid-wrapper'][1]/div[@class='yCmsContentSlot']/div[@class='login clear']/form[@id='loginForm']/div[@class='left sign-in']/button[@class='Sign-in rx-btn mb0']")).click();

    if((driver.findElement(By.xpath(".//*[@id='globalMessages']/div"))).isDisplayed())
    {
        System.out.println("Login Failed");
        String Error=driver.findElement(By.xpath(".//*[@id='globalMessages']/div")).getText();
        System.out.println("The Error mesaage is :"+Error);
        System.out.println("***********************************************************************************************************");
    }
    else
    {
        System.out.println("Login Sucessfull");
        System.out.println("***********************************");
        driver.findElement(By.xpath(".//*[@id='content-inner']/div[1]/div/div[2]/div[4]/div/div/ul/li[9]/a")).click();
    }
    driver.close();
}

等待元素可见:

如果需要,请先试试这个

try
{
    if((driver.findElement(By.xpath(".//*[@id='globalMessages']/div"))).isDisplayed())
    {
        System.out.println("Login Failed");
        String Error=driver.findElement(By.xpath(".//*[@id='globalMessages']/div")).getText();
        System.out.println("The Error mesaage is :"+Error);
        System.out.println("***********************************************************************************************************");
    }
}
catch (Exception e)
{
    System.out.println("Login Sucessfull");
    System.out.println("***********************************");
    driver.findElement(By.xpath(".//*[@id='content-inner']/div[1]/div/div[2]/div[4]/div/div/ul/li[9]/a")).click();
}

非常感谢按预期工作非常感谢如果条件不适用于isdisplayed used try catch and working Fine问题寻求调试帮助(“此代码为什么不工作?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该问题所需的最短代码。没有明确问题说明的问题对其他读者无效。请参阅:。
WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='globalMessages']/div")));
//then your code.....