java - retriving and accessing identical elements in Selenium -
i automating tests webapp selenium in java. 1 of test cases has use slider 2 points on (for choosing age range)
in html-code both points represented
the style-attribute left:
changes accordingly position of point on slider bar.
now problem facing:
i cannot hold on both of slider-elements @ same time. have tried:
pagefactory
@findall
, both css selectors , xpath:@findall({ @findby(xpath = "//slider/span") }) private list<webelement> agesliders;
driver.findby(...)
css , xpath selectors - globally , locally in method, in case there problems pagefactory element initialisation- getting parent element , iterating through children slider points
- some other combinations of above
whatever however, retrieves 1 element twice. example doing:
system.out.println(driver.findelement(by.xpath("//slider/span[@class='pointer'][1]"))) system.out.println(driver.findelement(by.xpath("//slider/span[@class='pointer'][2]")))
prints out 2 elements same id:
[[firefoxdriver: firefox on mac (ee9fc3d7-77fb-3042-9002-5316c1d94e89)] -> xpath: //slider/span[@class='pointer'][1]] [[firefoxdriver: firefox on mac (ee9fc3d7-77fb-3042-9002-5316c1d94e89)] -> xpath: //slider/span[@class='pointer'][2]]
which makes accessing both of them separately impossible. sliding both elements moves left one.
the same happens while iterating through agesliders
list above: seem left slider (= first 1 in dom?).
the time able operate right slider was, when accessed it, i.e. without trying touch other one:
@findall({ @findby(xpath = "//slider/span") }) private list<webelement> agesliders; (...) resetslider(agesliders.get(1))
however doing example
resetslider(agesliders.get(1)) resetslider(agesliders.get(0))
in above example moves left slider again... confusing o_o
i grateful tips or experiences, me solve issue.
i tried experiment following html:
<html> <body> <slider> <span class="pointer" style="left: 58.878px"></span> <span class="pointer" style="left: 187.024px"></span> </slider> </body> </html>
then ran following simple test code:
public class footest { @test public void foo() { webdriver driver = new firefoxdriver(); foopage page = new foopage(); pagefactory.initelements(driver, page); driver.get("file:///users/aarondavis/desktop/index.html"); (webelement elem : page.getpointers()) { system.out.println(elem); system.out.println(elem.getattribute("style")); } driver.quit(); } public static final class foopage { @findby(xpath = "//slider/span") private list<webelement> pointers; public list<webelement> getpointers() { return pointers; } } }
the output shows both elements being iterated over:
[[firefoxdriver: firefox on mac (fdbc9c63-a173-d44b-97be-c328210ce0bf)] -> xpath: //slider/span] left: 58.878px; [[firefoxdriver: firefox on mac (fdbc9c63-a173-d44b-97be-c328210ce0bf)] -> xpath: //slider/span] left: 187.024px;
what code using move slider (e.g. body of resetslider
method)? guess either there's issue in method or there other elements on page match //slider/span
might being retrieved. can check printing size of list (e.g. system.out.println(page.getpointers.size());
). possibility if there validation of sliders on page side (e.g. can't move right 1 left of left 1 and/or have distance, etc).
note: btw output posted above doesn't show same id ... sort of tostring:
[[firefoxdriver: firefox on mac (fdbc9c63-a173-d44b-97be-c328210ce0bf)] -> xpath: //slider/span]
is web element tostring. long dash-separated string not id of element rather of web driver. please note don't need use @findall...
list of elements. different semantic case. whether or not list of elements @findby
in page object determined if field annotating declared type of webelement
or list<webelement>
.
Comments
Post a Comment