-1

Suppose I have an HTML that looks like this:

<div class="first second">
    Right!
</div>
<div class="first second third fourth">
    Wrong!
</div>
<div class="first second">
    Right!
</div>

If I try to locate the first and third div, using css:

driver.find_elements_by_class_name("first, second")

Instead of getting 2 elements in my list, I get all the three divs because the second one actually contains those classes and some others... How can I restrict it, to locate only the <div>s with:

class = "first second"
2
  • what is the tag you have that contains first, second third tag also contains the same thing. Commented Jun 9, 2021 at 16:43
  • You can't use the Xpath? Commented Jun 9, 2021 at 16:43

2 Answers 2

1
div[class = 'first second']

should work. Cause it is a exact match.

use it with CSS_SELECTOR

driver.find_elements(By.CSS_SELECTOR , "div[class = 'first second']")
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this if you need to get them via xpath:

driver.find_elements_by_xpath(".//div[@class='first second']")

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.