Reputation: 33
I'm trying to get all 'id' elements in a certain web page. I can find the ul element that contains them, and I can even find a specific element by id (i.e. browser.find_element_by_id('380797')), but when I'm executing browser.find_all_elements_by_tag_name('id') the result is an empty list. I also tried by css or xpath, same result.
How can I get all id elements in the list? The html looks something like that:
Upvotes: 0
Views: 99
Reputation: 17
browser.find_elements_by_xpath("//div[@id="placards-container"]/ul/div[contains(@class,'placard')]
The contains()
method is the best approach for detecting the exact type of elements.
Upvotes: 1
Reputation: 4173
Use general attributes that are present in all the elements. The rule is: find all div's with id's from the div with placards-container id.
CSS:
browser.find_elements_by_css_selector("#placards-container div[id]")
or
browser.find_elements_by_css_selector("#placards-container ul>div[id]")
Xpath:
browser.find_elements_by_xpath("//div[@id='placards-container']//div[@id]")
or
browser.find_elements_by_xpath("//div[@id='placards-container']/ul/div[@id]")
Upvotes: 0
Reputation: 763
You may use execute_script. (better way in my opinion)
external_js = """
jsonData = [];
data = document.querySelectorAll("#placards-container > ul")[0];
for(var row of data["children"]){
JSON.stringify(jsonData.push({id : row.id}));
}
return jsonData;
"""
browser.execute_script(external_js)
Upvotes: 1