c# - How to determine if an element is matched by CSS selector? -
given selenium webdriver element instance, check if element matched given css selector. functionality similar jquery's is()
function.
i'm using .net
bindings.
example (suppose method called is
)
var links = _driver.findelements(by.cssselector("a")); foreach (var link in links) { if (link.is(".myclass[myattr='myvalue']")) // ... else // ... other thing }
is there kind of built-in stuff achieve this, or if not, can suggest possible implementation? i'm new selenium, , have no idea yet.
update
i see there no built-in way this. final goal implement methods jquery's parents(selector)
, closest(selector)
, suggestions appreciated more special case.
there no selenium method equivalent of jquery's $(...).is(...)
. however, dom offers matches. not complete replacement $(...).is(...)
since not support jquery's extensions css selector syntax but, again, selenium not support these extensions either.
you'd use passing element want test javascript script pass executescript
. element appear first element of arguments
inside script. call matches
on , return value. don't c# based on documentation, believe in c#:
bool isit = (bool)(driver ijavascriptexecutor).executescript( "return arguments[0].matches(\".myclass[myattr='myvalue']\")", element);
isit
contains result of test. element
element want test, , driver
driver you've created. see caniuse compatibility. in applications, use polyfill provide matches
on platforms care about.
this being said i not recommend looping on elements , testing them 1 one. problem each executescript
or getattribute
call round-trip between script , browser. when run full test suites, adds up, if browser run on server farm away script. really ads up. structure test queries list of a
elements , list of a.myclass[myattr='myvalue']
elements. boils down 2 round-trips. 2 lists have information need if()
test.
Comments
Post a Comment