Ke.
Ke.

Reputation: 2586

selenium, iterate over list, finding the selector

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)

Upvotes: 0

Views: 1119

Answers (1)

Ke.
Ke.

Reputation: 2586

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)

Upvotes: 1

Related Questions