How to find text on a webpage and get its XPath or CSS with Selenium and Java -
How to find text on a webpage and get its XPath or CSS with Selenium and Java -
there page lists items on dynamic table. order , number of items on table changes randomly.
i find 1 of items "itemx" based on text "itemx" , , utilize xpath or css in part of code.
i can find element based on text "itemx"
driver.findelement(by.cssselector("body")).gettext().matches("^[\\s\\s]*itemx[\\s\\s]*$");
but how can xpath or css?
i can't seem find on web. out there shows how verify or find text using xpath. me it's opposite. have text , want find xpath.
both xpath , css selectors arbitrary. there no reply in general case.
selecting first c
in <a><b></b><c></c><c></c></a>
css selector might c:first-of-type
, or a > c:nth-child(2)
, or a c:first-of-type
, or a > b + c
, or number of other things. xpath there many ways of representing it. if alter markup, should happen?
it depends upon context, stability required, whether false positives or false negatives worse (you can create strict if markup changes break, or can create loose if markup changes may select wrong thing) , whim of person doing it. (which of ways above select? eeny, meeny, miny, mo...)
if, however, care current state of document , have no need resilience, first of consider if need have such selector. can pass dom element around instead. if want uniquely identify element, though, next through dom tree going straightforward way, travelling downwards level level, observing kid node is. you'll end this:
:root > :nth-child(3) > :nth-child(1) > :nth-child(4)
ugly, may want.
java css xpath selenium
Comments
Post a Comment