1

Needed some help with pattern matching in Selenium. I am trying to read the following inner div tags,

<div>
     <div class="xyz one"></div>

     <div class="xyz two"></div>

     <div class="xyz three"></div>
</div>

Is there a way I can do this? Any suggestions would be much appreciated.

Cheers

Trott
  • 66,479
  • 23
  • 173
  • 212
konkani
  • 488
  • 1
  • 7
  • 15

3 Answers3

4

Here's an XPath that you can use in order to select all div tags that contain the text 'xyz'.

"//div[contains(@class, 'xyz')]"

So in order to use this, you'd use this line, which creates a collection of all the divs with the text 'xyz'

var divElements = driver.FindElements(By.XPath("//div[contains(@class, 'xyz')]"));
Michael Bautista
  • 1,220
  • 12
  • 19
2
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.CssSelector("div.xyz"));
iMatoria
  • 1,450
  • 2
  • 19
  • 35
1

Looks like your question has already been answered Get HTML Source of WebElement in Selenium WebDriver using Python Then you can parse your output.

EDIT

To make things easier: (Using Python)

Try:

class_list = [] 
my_divs = driver.find_elements_by_css_selector(div[class*=xyz])
for div in my_divs:
   outerHTML = div.get_attribute("outerHTML")
   classpos = outerHTML.find('class=') #find where our class identifier begins
   quote_begin = outerHTML.find('"',classpos) 
   quote_end = outerHTML.find('"', quote_begin+1)
   our_class = outerHTML[classpos:quote_end+1] #get the entire class identifier
   class_list.append(our_class)
   print our_class

I'm sure theres someone with a one liner of code that does the same thing that my 4 lines does but this was what I could come up with. Also, this code can be changed around pretty quickly into Java

Community
  • 1
  • 1
Greg
  • 5,422
  • 1
  • 27
  • 32