我的硒代码在这里
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);
}
并且所有driver().findElement(By.xpath())
都工作正常,直到我需要在循环中重复填写表单,然后突然我开始收到NoSuchElementException
错误。
我不明白为什么我收到错误,当我所在的网页上有所有字段时,我直接从devtools中的[right click]->[copy xpath]
获取了xpath。
字段肯定在那里,我使用的XPATH是正确的,我已经多次检查,所以有我的同事,但我不知道为什么它到达循环之前,然后停止工作。
我也试过findElement(By.id())
,但仍然给了我同样的错误。 Selenium就好像这些元素不存在,即使它们存在。
答案 0 :(得分:0)