Aditya Sharma
Aditya Sharma

Reputation: 11

Python - Selenium (find elements by CSS selector)

How do I access the first element of an array using a CSS selector in python?

For Example:

css=('.od-FieldEditor-fieldTitle.ms-Label.is-required') returns an array of 6 labels. I want to access the innerText attribute of the first label.

Image shows the array of labels

Upvotes: 0

Views: 2349

Answers (1)

Abe
Abe

Reputation: 1415

Try this:

elements = driver.find_elements_by_css_selector('selector_required')


# if you want to navigate through
for e in elements:
    # do some stuffs with 'e'

# to access a single object
e = elements[0] # will return the first element of a list

Upvotes: 1

Related Questions