简体   繁体   中英

Selenium WebDriver - Dropbox selection Java

Hi all

Sorry for any problems. I'm new in WebDriver so i would be great full for any help If you have any goo tutorial, which i can use thank you for that.

So lets get into my problem:

I have got a dropbox and a try to pick every value and confirm that with button then get a warning message ....

Here is my code:

public void specialniBudovyVsechny() throws IOException{
        try{
            driver.findElement(By.xpath("//*[@id='budovy']/a")).click();
            driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody/tr[2]/td/strong[2]/a")).click();    
            Select listItem=new Select(driver.findElement(By.xpath("//*[@id='main']/table[2]/tbody/tr[2]/td[1]/select")));
            String[] dropdown = new String[listItem.getOptions().size()];
            WebElement element = driver.findElement(By.xpath("//*[@id='main']/p"));
                for(int i=0;i<listItem.getOptions().size();i++)
                {
                   listItem.selectByIndex(i);   
                   dropdown[i]  = listItem.getFirstSelectedOption().getText();
                }


                for(int a = 0;a<dropdown.length ;a++){
                    if(a == 0 | a == 1 | a == 12 | a == 13){
                        listItem.selectByVisibleText(dropdown[a]);
                        driver.findElement(By.xpath("//*[@id='main']/table[2]/tbody/tr[2]/td[2]/input")).click();
                        Assert.assertEquals(element.getText(), "Nemáš žádnou rozestavěnou budovu"); 
                    }
                    else{
                        listItem.selectByVisibleText(dropdown[a]);
                        driver.findElement(By.xpath("//*[@id='main']/table[2]/tbody/tr[2]/td[2]/input")).click();
                        Assert.assertEquals(element.getText(), "Tvá rozestavěná budova: "+dropdown[a]+" - 0%");

                    } }     }
        catch (Exception vyjimka){
            getScreenShot();
            System.out.println("SpecialniBudovyVsechny");
            System.out.println(vyjimka);
            }

Everytime i gets this error: org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout: 11 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52' System info: host: 'N0119', ip: '192.168.0.14', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=41.0.2, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 8634cbaf-70db-4b07-a19f-b040ba5c60bd

I found out that the issue is array but i dont know how to solve that. If i place right array for example dropdown[2]. It works

Thank you

As a suggestion you can try to put this:

Select listItem=new Select(...

inside your loops.

I cleaned up your code a bit. There were a few issues. I think I got it put back together correctly but I have no way to test it.

  1. You were storing all the OPTION values in a string array and then looping through the array. There's no need to do that... you can just loop through the options and compare the text as you go. Saves you having to loop through twice.

  2. In your if..else you were executing the same two lines of code in both branches. To clean this up, just pull those two lines before the if..then to prevent redundancy.

  3. You were using | instead of || . I think you meant to use logical OR which is || . See this page for more info.

Here's the cleaned up code. Give this a try and let me know if you run into any issues.

public void specialniBudovyVsechny() throws IOException
{
    try
    {
        driver.findElement(By.xpath("//*[@id='budovy']/a")).click();
        driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody/tr[2]/td/strong[2]/a")).click();
        Select listItem = new Select(driver.findElement(By.xpath("//*[@id='main']/table[2]/tbody/tr[2]/td[1]/select")));
        WebElement element = driver.findElement(By.xpath("//*[@id='main']/p"));

        for (int a = 0; a < listItem.getOptions().size(); a++)
        {
            listItem.selectByIndex(a);
            driver.findElement(By.xpath("//*[@id='main']/table[2]/tbody/tr[2]/td[2]/input")).click();
            if (a == 0 || a == 1 || a == 12 || a == 13)
            {
                Assert.assertEquals(element.getText(), "Nemáš žádnou rozestavěnou budovu");
            }
            else
            {
                Assert.assertEquals(element.getText(), "Tvá rozestavěná budova: " + listItem.getFirstSelectedOption().getText() + " - 0%");
            }
        }
    }
    catch (Exception vyjimka)
    {
        getScreenShot();
        System.out.println("SpecialniBudovyVsechny");
        System.out.println(vyjimka);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM