Selenium Css Locator All Attribute Options With And Without Being Strict
Solution 1:
For disabled elements there is disabled pseudo-class:
css=input:disabled[id="tbd"][type="text"]
From that document seems you really can't select by onclick and other events (which is logical, css is responsible for representation, not for js events). Why not use XPath for that?
Your last question could be rewritten as follows I suppose:
css=input[type="text"]
to match this HTML <input type="text">
and not this <input type="text" disabled="disabled">
AFAIK there is no such "strict" matching. You need to specify full selector that will match only needed elements: css=input:enabled[type="text"]
or css=input:not(:disabled)[type="text"]
.
Solution 2:
Use xpath for complex selectors, it will save you a lot of time and code.
This is an example of xpath selectors usage, but using different library for brevity
(BY_ID_XPATH
and BY_ID_AND_NOT_DISABLED_XPATH
xpath expressions will be the same for your selenium code):
from lxml import etree
HTML = """
<input id="tbd" type="text" disabled="disabled">
<input id="tbd" type="text">
"""
BY_ID_XPATH = '//input[@id="tbd"]'
BY_ID_AND_NOT_DISABLED_XPATH = '//input[@id="tbd" and not(@disabled)]'
elements_tree = etree.fromstring(HTML, parser=etree.HTMLParser())
elements_tree.xpath(BY_ID_XPATH)
# matches 2 elements: [<Element input at 102ea1d08>, <Element input at 102ea1db8>]
elements_tree.xpath(BY_ID_AND_NOT_DISABLED_XPATH)
# matches 1 element: [<Element input at 102ea1d08>]
id
attribute should be unique across the whole web page. However this is not always the case, since nothing can stop developer to use more than one identical id
value.
If you have any power in the project – you can use id
as unique attribute to keep automation code short and clean, and file bugs like "Duplicated id attribute on the Home page".
For onclick
matching your selector will look like this:
BY_ONCLICK_XPATH = """//input[@onclick="javascript:alert('button1')"]"""
Post a Comment for "Selenium Css Locator All Attribute Options With And Without Being Strict"