-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"
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
James Deen
  • 23
  • 3

2 Answers2

0
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']")
cruisepandey
  • 26,802
  • 6
  • 16
  • 37
0

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

driver.find_elements_by_xpath(".//div[@class='first second']")
itronic1990
  • 1,084
  • 2
  • 3
  • 16