2016-06-10 10 views
1

Ich habe eine SelenWebelement-Liste, die drei Webelement-Objekt enthält. Ich möchte den Index jedes Elements mit For-Schleife abrufen. Wie kann ich es in Python tun?Python: Wie über Selen Webelement Liste und Index erhalten?

derzeit wie dies tue:

countries=Select(self.driver.find_element_by_xpath('//[@id="id_country"]')).options 
for index, value in countries: 
    print index 

aber es gibt mir Fehler

TypeError: 'WebElement' object is not iterable 
+0

könnten Sie was Sie tatsächlich versucht haben? – sumit

+0

Ich habe mehr Erklärung hinzugefügt –

Antwort

0

Versuchen Sie das folgende Beispiel

select_box = browser.find_element_by_xpath('//[@id="id_country"]') 
options = [x for x in select_box.find_elements_by_tag_name("option")] #this part is cool, because it searches the elements contained inside of select_box and then adds them to the list options if they have the tag name "options" 
for element in options: 
    print element.get_attribute("value") # or append to list or whatever you want here 
1

Verwenden enumerate mit der for Schleife:

countries=Select(self.driver.find_element_by_xpath('//[@id="id_country"]')).options 
for index, value in enumerate(countries): 
    print index 

Das

0 
1 
2 

Sie können auch den Startindex angeben, wenn Sie es Null indiziert werden nicht wollen, drucken würde:

countries=Select(self.driver.find_element_by_xpath('//[@id="id_country"]')).options 
for index, value in enumerate(countries, 10): 
    print index 


10 
11 
12 
Verwandte Themen