kroe761
kroe761

Reputation: 3534

Selenium webdriver, Python - target partial CSS selector?

I am trying to target specific CSS elements on a page, but the problem is that they have varying selector names. For instance, input#dp156435476435.textinput.wihtinnextyear.datepicker.hasDatepicker.error. I need to target the CSS because i am specifcally looking for the .error at the end of the element, and that is only in the CSS (testing error validation for fields on a website. I know if I was targeting class/name/href/id/etc, I could use xpath, but I'm not aware of a partial CSS selector in selenium webdriver. Any help would be appreciated, thanks!

Upvotes: 2

Views: 3503

Answers (3)

user3487861
user3487861

Reputation: 350

css=span.error -- Error

css=span.warning -- Warning

css=span.critical -- Critical Error

Simple above are the CSS Selectors we can use.

Upvotes: 0

sam2426679
sam2426679

Reputation: 3837

If the page has similar elements, you can combine what Arran said like so:

driver.find_element_by_css_selector( "input[id^='dp'][class*='nextyear'][class$='error']" )

Upvotes: 0

Arran
Arran

Reputation: 25056

The period/full stop is a partial class selector:

span.error

would find:

<span class="error" />
<span class="error warning" />
<span class="critical error" />

You also have:

span[class^='error']

(begins with)

span[class$='error']

(ends with)

span[class*='error']

(contains)

Upvotes: 3

Related Questions