0

Right now, my code screenshots a specific section of a webpage. However, since the element it's screenshotting is quite small on the webpage, the picture is also quite small. So, to solve this issue, I thought I could zoom into the page, center onto the element, screenshot the element, and then zoom out – but it's not working.

When I run the following code (no zooming in):

theComment = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[6]/div/div/div/div[1]")
driver.execute_script('arguments[0].scrollIntoView({block: "center"});', theComment)
theComment.screenshot('comment'+str(verified+1)+'.png')

I get the following image:

enter image description here

But when I run the following code (zooming in):

theComment = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[6]/div/div/div/div[1]")
driver.execute_script("document.body.style.zoom='150%'")
sleep(1)
driver.execute_script('arguments[0].scrollIntoView({block: "center"});', theComment)
sleep(1)
theComment.screenshot('comment.png')
sleep(1)
driver.execute_script("document.body.style.zoom='100%'")

I get the following image (the screenshot is really far away from where the actual comment is, and smaller than the comment is):

enter image description here

Any help would be greatly appreciated. Thank you.

  • Does the zooming change the layout of the page dynamically in such a way that elements are arranged differently and the xpath actually points to a different element? It shouldn't, but your list of `div`s, some with indices, looks extremely fragile. – Raketenolli May 26 '22 at 18:33
  • @Raketenolli I don't think so. Here's the webpage I'm going through: https://www.reddit.com/r/AskWomen/comments/uxnffe/whats_your_biggest_turn_off/ – THEMOUNTAINSANDTHESKIES May 26 '22 at 18:43
  • 1
    if this is still true then it seems not so easy to do https://stackoverflow.com/a/61047545/8157304 – sound wave May 27 '22 at 05:39

0 Answers0