0

I'm trying to find the property of a webelement in selenium, that gives me the selector of the webelement.

I realise that the following properties are available for a webelement:

tag_name
text
size
location
parent
id

I'm just wondering if there is any way to get the id or class_name of a particular element I am iterating over. For instance, I have the following selenium (I'm using selenium base, so ignore the "open" function)

    self.open('file:///myfile.htm')# Opens the url
    self.header = self.driver.find_element_by_class_name("my-class-name")
    name = self.driver.find_elements_by_css_selector('*')
    self.process_checks()
    for index in range(0,3):
        print name[index].id

I can iterate over the elements in the element "my-class-name". However, none of these properties are giving me the actual selector name. Is there any way to identify that, so I can do the following:

if name[index].selector = 'my-div-inside-class-name': doStuff()

get_attribute is not what I needed. It's much easier to do the following:

    #Limit xpath to the surrounding div        
    self.header = self.driver.find_element_by_class_name("surrounding-div")
    #Get elements inside surrounding div, with "elements-i-need" in the class name
    self.name = self.header.find_elements_by_class_name('elements-i-need')
    for index in range(len(self.name)):
        #You can print each found element here.
        print(self.name[index].text)
Ke.
  • 2,484
  • 8
  • 40
  • 78
  • Possible duplicate of [How to get attribute of element from Selenium?](http://stackoverflow.com/questions/30324760/how-to-get-attribute-of-element-from-selenium) – JeffC Mar 26 '17 at 15:29

1 Answers1

1

As mentioned in the answer this is how I did it:

#Limit xpath to the surrounding div        
self.header = self.driver.find_element_by_class_name("surrounding-div")
#Get elements inside surrounding div, with "elements-i-need" in the class name
self.name = self.header.find_elements_by_class_name('elements-i-need')
for index in range(len(self.name)):
    #You can print each found element here.
    print(self.name[index].text)
Ke.
  • 2,484
  • 8
  • 40
  • 78