Reputation: 303
I can't figure out the syntax to return the specific elements in this HTML example.
<div class="calibre" id="calibre_link-0">
<div class="book" title="Chapter 11. Web Scraping">
<div class="titlepage">
<div class="book">
<div class="book">
<h1 class="title1"><a id="calibre_link-2915" class="firstname"></a>Web Scraping</h1>
</div>
</div>
</div>
What I have tried is driver.find_elements_by_tag_name('div.calibre.div')
and also using
('div.calibre .div') | ('div.calibre + div')
and other variations on those. I have managed to return all the div elements in the page but I just want to return all of the ones nested within the calibre class (i.e. book, titlepage, book, and book). Is there a way to do this?
Upvotes: 5
Views: 13136
Reputation: 8328
If there's only one element with the class 'calibre' and it's the one you need, you can do:
driver.find_element_by_class_name('calibre').find_elements_by_tag_name('div')
When you execute driver.find_element_by_class_name('calibre')
(or any other driver.find...
function for that matter), that call returns an object that has the same methods as driver. In other words, you get a piece of the webpage that you can navigate as you would the whole webpage.
In this case, driver.find_element_by_class_name('calibre')
will return a portion of the webpage with de element whose class is 'calibre' and all it's children. To access it's divs, you should navigate it with find_elements_by_tag_name
.
Upvotes: 6