0

I am using Selenium to parse a page containing markup that looks a bit like this:

<html>
    <head><title>Example</title></head>
    <body>
        <div>
            <span class="Fw(500) D(ib) Fz(42px)">1</span>
            <span class="Fw(500) D(ib) Fz(42px) Green XYZ">2</span>
        </div>
    </body>
</html>

I want to fetch all span elements that contain the class foobar.

I have tried both of this (the variable wd is an instance of selenium.webdriver):

elem = wd.find_elements_by_css_selector("span[class='Fw(500) D(ib) Fz(42px).']")

elem = wd.find_element_by_xpath("//span[starts-with(@class, 'Fw(500) D(ib) Fz(42px))]")

NONE OF WHICH WORK.

How can I select only the elements that start with Fw(500) D(ib) Fz(42px)

i.e. both span elements in the sample markup given.

2 Answers 2

2

Try as below :-

elem = wd.find_elements_by_css_selector("span.foobar")

If there is space between class foo and bar then try as below :-

elem = wd.find_elements_by_css_selector("span.foo.bar")

Edited : If your class contains with non alphabetical charactor and you want to find element which starts with Fw(500) D(ib) Fz(42px) then try as below :-

elem = wd.find_elements_by_css_selector("span[class ^= 'Fw(500) D(ib) Fz(42px)']")
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, I gave a bad example. My foobar actually contains spaces. I'll modify the question to reflect that
@HomunculusReticulli means foo bar??
@HomunculusReticulli ok then try updated answer with elem = wd.find_elements_by_css_selector("span.foo.bar")
Sorry, my example was still wrong. I've corrected it and I'm using the actual class name now. Your code will not work, because my class name contains "non standard" characters - the page is generated by a React application.
@HomunculusReticulli Ok, now if you want to find element which element class start with Fw(500) D(ib) Fz(42px)...try updated answer..:)
-1

Try to find elements by XPath:

//span[@class='foobar']

This should work.

1 Comment

To be fair (to whomever marked this answer down). The question has since changed when this answer was given, and I have removed all references to foobar from my original question (see discussion with Saurabh Gaur below).

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.