Take a Select Enterprise page as an example, assuming that there is no prior knowledge of the user login will have several enterprises to choose, but the case also need to traverse the enterprise, to ensure that each enterprise can be properly accessed, then you can be processed in red marked way.
public class Chooseenterprise {
Private Webdriver driver;
Private String BaseUrl;
Private StringBuffer verificationerrors = new StringBuffer ();
@Before
public void SetUp () throws Exception {
System.setproperty ("Webdriver.ie.driver", "C:\\Program Files (x86) \\Internet Explorer\\iedriverserver.exe");
Driver = new Internetexplorerdriver ();
BASEURL = "http://xx.xxx.cn/";
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);
}
@Test
public void Testchooseenterprise () throws Exception {
int i = 1;
int j = 1;
String str = "";
String strlast = "";
//because I don't know what I need to openHow many links there are, so use whileLoop, always giving a trueValue until the condition satisfies the bounce loop
while (i!=0) {
Driver.get (BaseUrl + "xx/xx/login.jsp");
//determine if you have successfully entered the login page
try {
Assertequals ("UseMobile Account Login", Driver.findelement (By.cssselector (" Span.fontred2 ")). GetText ());
} catch (Error e) {
Verificationerrors.append (E.tostring ());
}
Driver.findelement (By.id ("Usersphone")). Clear ();
Driver.findelement (By.id ("Usersphone")). SendKeys ("13800000000");
Driver.findelement (By.id ("Userspassword")). Clear ();
Driver.findelement (By.id ("Userspassword")). SendKeys ("123456");
Driver.findelement (By.cssselector ("img")). Click ();
//determine whether to reach the Select Enterprise page
try {
Assertequals ("Select Enterprise", Driver.findelement (By.xpath ("//h3 ")). GetText ());
} catch (Error e) {
Verificationerrors.append (E.tostring ());
}
Gets the property ofCLASS=QY_BGThe DIVElementsThe value
str = driver.findelement (By.xpath ("(//div[@class = ' qy_bg ')" ["+i+"] "). GetText ();
Gets the property ofCLASS=QY_BGThe DIVlast elementThe value
Strlast = Driver.findelement (By.xpath (//div[@class = ' qy_bg ') [Last ()]]). GetText ();
Driver.findelement (By.xpath ("(//a[contains (Text (), '" +str+ "')])). Click ();
//determine whether to enter the Enterprise homepage
try {
Assertequals (str, driver.findelement (By.xpath ("//p[2]")). GetText ());
} catch (Error e) {
Verificationerrors.append (E.tostring ());
}
//Click to enter personalCenter icon to exit the operation
Driver.findelement (By.cssselector ("DIV.R_SZ > A > img"). Click ();
//check whether or not to enter a personalCenter
try {
Assertequals ("PersonalCenter", Driver.gettitle ());
} catch (Error e) {
Verificationerrors.append (E.tostring ());
}
//Click to exit
Driver.findelement (By.linktext ("Exit")). Click ();
//Check whether the exit was successful
try {
Assertequals ("Welcome to log in to network letter", Driver.findelement (By.cssselector (" p.fontred ")). GetText ());
} catch (Error e) {
Verificationerrors.append (E.tostring ());
}
J can count the total number of enterprises, for the test results output use
j = i;
System.out.println (j);
System.out.println (str);
when the value of str equals strlast, it means that the last link has been reached, at which point you can jump out of the loop
if (Strlast.equalsignorecase (str)) {
Break
}
i++;
}
}
@After
public void TearDown () throws Exception {
Driver.quit ();
}
}
Selenium Webdriver to complete the traversal of all elements of the page without knowing the number of elements