1

I'm trying to parsing text from this html code using Selenium with python:

    <div class="  card card_selected" data-id="3248846777">
        <p>Hello World!</p>
    </div>

I use this code:

my_class = "  card card_selected"
live_deal = browser.find_element_by_class_name(my_class)

I get this error :

selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression ".  card card_selected" is invalid: InvalidSelectorError: '.  card card_selected' is not a valid selector: ".  card card_selected"

This error happen when the class name start with spaces.

how can I handle this error ?

1
  • Please show us the full code. Commented Dec 13, 2019 at 14:17

2 Answers 2

2
browser.find_element_by_css_selector('[class|=" "]')

See more details here: https://www.w3schools.com/css/css_attribute_selectors.asp

CSS [attribute|="value"] Selector
The [attribute|="value"] selector is used to select elements with the specified attribute starting with the specified value.

Sign up to request clarification or add additional context in comments.

Comments

0

Might be easier to use xpath contains() function

//div[contains(@class,'card_selected')]

Comments

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.