简体   繁体   中英

Why can't selenium webdriver see the elements on the webpage? (NoSuchElementException)

I have my selenium code here

        System.out.println("Selects selected");

        Methods methods = new Methods();

        /**
            there's some code here, but it's just a lot of driver().findElement() and updating values in fields. It works OK
        **/

        driver().findElement(By.xpath("//*[@id=\"updateclient\"]")).click();

        Thread.sleep(5000);

        driver().findElement(By.xpath("//*[@id=\"quoteclient\"]")).click();

        Thread.sleep(15000);

        /* Selects the Zurich Quote (Only one currently working) */
        driver().findElement(By.xpath("//*[contains(@id, 'quote-0')]//*[contains(@alt, 'Zurich')]")).click();

        /* Select the Apply for Big 3 CIC button */
        driver().findElement(By.xpath("//*[@id=\"apply_for_big3\"]")).click();

        //Just wait for the page to load properly
        Thread.sleep(2500);

        /**
         * Now we are at the big3 eligibility page. We will need to enter the client details.
         * Because we want to check all the outcomes that iptiQ have given us, we're going to do
         * this in a loop.
         *
         * This loop will set the client details, answer the questions, and then go through to the
         * quoting page.
         *
         * When the client has been quoted for the values set, we will check that the premium matches
         * the expected value.
         *
         * At the end of each iteration, the user will be returned to the Big3 eligibility screen so that
         * the next set of values can be set.
         */

        //Get the fields. -------
        //This is the bit that keeps failing and it should not be failing
        // because the elements are all present on the page
        WebElement EligibilityTitleOne = driver().findElement(By.xpath("//*[@id=\"title_1\"]"));
        WebElement EligibilityForenameOne = driver().findElement(By.xpath("//*[@id=\"forename_1\"]"));
        WebElement EligibilitySurnameOne = driver().findElement(By.xpath("//*[@id=\"surname_1\"]"));

        String[][] SumAssuredCases = QuoteGraph.SingleLifeSumAssured;

        for(int i=0; i<SumAssuredCases.length; i++){

/**
            //Extract all the required values from the array
            int AgeNextBirthDay = Integer.parseInt(SumAssuredCases[i][1]);
            String SmokerStatus = SumAssuredCases[i][2];
            String SumAssured = SumAssuredCases[i][5].replace(",","");
            String PolicyTerm = SumAssuredCases[i][6];
            String ExpectedPremium = SumAssuredCases[i][7];
            String ExpectedCommission = SumAssuredCases[i][8];
 **/

int AgeNextBirthDay = 25;
String SmokerStatus = "NonSmoker";
String SumAssured = "10000";

            //We are going to use a pre set name, as names do not affect the premium.
            EligibilityTitleOne.clear();
            EligibilityTitleOne.sendKeys("Mr");
            System.out.println("Set customer 1 title");
            EligibilityForenameOne.clear();
            EligibilityForenameOne.sendKeys("Tester");
            System.out.println("Set customer 1 forename");
            EligibilitySurnameOne.clear();
            EligibilitySurnameOne.sendKeys("Testeez");
            System.out.println("Set customer 1 surname");

            //Now we are going to set the sex to be male. This does not affect the premium.
            Select clientOneSexSelect = new Select(driver().findElement(By.xpath("//*[@id=\"gender_1\"]")));
            clientOneSexSelect.selectByVisibleText("Male");
            System.out.println("Set customer 1 to male");

            //Now we need to set the smoker status from the value in the customer profile
            Select clientOneSmokerStat = new Select(driver().findElement(By.xpath("//*[@id=\"smoker_1\"]")));
            if(SmokerStatus.matches("Smoker")) {
                clientOneSmokerStat.selectByVisibleText("Yes");
                System.out.println("Customer 1 is a smoker.");
            } else {
                clientOneSmokerStat.selectByVisibleText("No");
                System.out.println("Customer 1 is not a smoker.");
            }

            //Now we need to calculate the date of birth
            String customerOneDob = methods.DOBFromAge(AgeNextBirthDay);
            driver().findElement(By.xpath("//*[@id=\"dob_1\"]")).sendKeys(customerOneDob);
            System.out.println("Customer 1 date of birth set to " + customerOneDob);

            //Now we need to set the sum assured value
            driver().findElement(By.xpath("//*[@id=\"sum_assured\"]")).clear();
            driver().findElement(By.xpath("//*[@id=\"sum_assured\"]")).sendKeys(SumAssured);
            System.out.println("Sum assured set to £" + SumAssured);

            //Select the update client details
            driver().findElement(By.xpath("//*[@id=\"update_lead\"]")).click();

            //Wait for update to complete
            Thread.sleep(1000);
            System.out.println("The changes have been saved.");

            Thread.sleep(2500);
        }

and all the driver().findElement(By.xpath()) have worked fine up until the point when I need to fill out the form repeatedly in the loop, and suddenly I start getting the NoSuchElementException error.

I don't understand why I am getting the error, when the webpage I am on has all the fields, I have taken the xpaths directly from [right click]->[copy xpath] in devtools.

The fields are definitely there and the XPATH I am using is correct, I've checked multiple times and so have my colleages, but I don't know why it gets to just before the loop and then stops working.

I've also tried findElement(By.id()) but that still gave me the same error. Selenium is acting as though the elements don't exist, even though they do.

  1. Check if the elements are with in IFrame then use driver.switchto.frame (frmaelocator);
  2. If it's on a different tab use windowhandles

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