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)
1

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

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.