Reputation: 135
My goal is to be able to find the sizes & location on page of all of the Instagram (post+comment) blocks of users I follow. (large rectangle block of page below with picture on left and comments on right)
An example of this is the following:
click on the first post
driver.find_element
the image with comments block in the center of the page above so that I can find the size & location on page of the block. The xpath I see is (//*[@id="react-root"]/section/main/div/div/article/div[1]
) and the div class begins with _97aPb
, however, when I refresh or go to a different user these change. So, I'm looking for an alternative way to find that element. I want to be able to click to the next post and be able to find the (post +comment) block for all posts.
Upvotes: 0
Views: 427
Reputation: 1597
How to get the article:
Depending if how opened the post, the contents will be under the div with @role="dialog"
or @id="react-root"
. If you open the post directly, there is no dialog but if you search for '//div[@role="dialog" or @id="react-root"]//article
then the last one will be the post you are looking for.
So:
article = driver.find_elements_by_xpath('//div[@role="dialog" or @id="react-root"]//article')[-1]
How to get the next image in the article:
next_image_arrow = article.find_element_by_xpath('.//*[contains(@class, "coreSpriteRightChevron")]')
How to select the next article (available only when article opened in modal):
next_article_button = driver.find_element_by_xpath('//a[contains(@class, "coreSpriteRightPaginationArrow")]')
How to get the comments:
all_comments = article.find_elements_by_xpath('./div/section[2]/following-sibling::div/ul/ul')
The Xpath above means this:
/div/section[2]
<< in the article node, get all nodes which have a direct div
child node which have at least 2 direct section
tagged child nodes and select the second one/following-sibling::div/ul/ul
<< take the found section nodes and if the section node is followed by a div and it has direct ul child nodes which have direct ul child nodes, return themI hope this answers the question.
Upvotes: 1