file - Search a link on webpage and click on it using selenium -
i using selenium webdriver. have read xpath of link file , search whether link present on webpage, if present click on it. thats it!!
heres file links
link1 //a[contains(text(), 'volunteer registration')]/@href link2 //a[contains(text(), 'sign up')]/@href link3 //a[contains(text(), 'register/sign up')]/@href
like wise have 1 file i'll read 1 link , associated xpath , based on xpath i'll search whether link present on webpage or not.
the code have written :
reading data text file hashtable -
public hashmap<string, string> readdatafromfile(string filename) { try { filereader fr = new filereader(filename); bufferedreader br = new bufferedreader(fr); string strline = null; string[] prop = null; while ((strline = br.readline()) != null) { prop = strline.split("\t"); recruiters.put(prop[0], prop[1]); } br.close(); fr.close(); } catch (exception exception) { system.out.println("unable read data recruiter file: " + exception.getmessage()); } return recruiters; }
method return xpath value hashtable based on key
public string findvalue(string name){ (string s: hashtable.entryset()) { map.entry entry = (map.entry) s; if(entry.getkey().equale(name)) { string value= entry.getvalue(); return value; } } return null; }
now want write method search whether xpath related link present on webpage or not..
please me that..
the logic like
public void search&clicklink() { list<webelement> links = driver.findelements(by.tagname("a")); system.out.println(links.size()); (webelement myelement : links){ string link = myelement.gettext(); system.out.println(link); myelement.click(); driver.navigate().back(); }
but not sure it. please let me know if approch correct if function approprite. please suggest better way implement code.
thanks!!
well...there couple of problems last set of code.
the first going staleelementreferences. when find element (or list of elements), pointing element page. if refresh page or leave , come back, not valid, , have re-find of elements.
also, many times link doesn't navigate new page. if case of links, find clicking links on wrong page (because navigated back)
finally, aren't doing on page. know, link go 500 error, , selenium have no idea.
however, since have of links in file, why not read file, store in array, , simple loop:
for (string linkname: alllinks){ driver.get(urlwithlinks); driver.findelement(by.linktext(linkname)).click(); ...validate page... }
lastly...i believe clicking on of links on page terrible test. better test go link, , stuff on page. way testing functionality of website.
Comments
Post a Comment