1

I am trying to access the text of multiple tags using selenium in python.

The tags do not have attribute like id or class; they have an attribute named itemprop.

For instance there are multiple tags of such type:

<p itemprop="articleBody">
London's Gatwick Airport ........</p>

I can't use "select element by tag name" because there are tag "p" with different attributes which I don't want to include.

I am using the below code to select these elements:

elements = driver.find_element(By.CSS_SELECTOR, """p[itemprop='articleBody’]""")

However it throws the error - ......

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"p[itemprop='articleBody’]"}

How can I fix this?

1
  • Corrected Instance For instance there are multiple tags of such type (p itemprop="articleBody")....London's Gatwick Airport ........(/p) Commented Dec 22, 2018 at 13:43

1 Answer 1

1

you have smart quote in the selector, use find_elements* with s to get multiple elements.

elements = driver.find_elements(By.CSS_SELECTOR, 'p[itemprop="articleBody"]')
# Or
elements = driver.find_elements_by_css_selector('p[itemprop="articleBody"]')
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. It worked. However, I did not understand the difference why single quotes did not work?
you can use single quotes and it different with the smart quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.