4

See below where I try to screenshot Facebook. The top bar which is normally at the top of the browser, is appearing in the middle of the screenshot.

How can I hide it, or adjust the browser so that the top bar stays up and out of the way?

I use Selenium to screenshot then I sent it to PIL to get it cropped.

Note that when this appears, I may be scrolled down quite a ways.

enter image description here

User
  • 23,729
  • 38
  • 124
  • 207

3 Answers3

4

You can manipulate the html code like deleting the class attribute of the navigation bar by this driver.execute_script("document.getElementById('blueBarNAXAnchor').setAttribute('class', '')") then it stay in the begining of the page without style. Then you can take screenshot without it. See the below, check face_1.png:

>>> driver.save_screenshot("face.png")
True
>>> driver.execute_script("document.getElementById('blueBarNAXAnchor').setAttribute('class', '')")
>>> driver.save_screenshot("face_1.png")
True

enter image description here

Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
2

Possibly it has to do with the fact that the browser wasn't opened with maximum size.

Try calling driver.maximize_window() once at initialization before using driver.get and driver.save_screenshot.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
2

Instead of increasing the window size your should reduce the window size. Set the window height to less than 500px and the top bar gets placed in the screenshot where it should be.

This is because when the browser height is below 500px, the top bar disappears from view. Above this height the top bar appears in the browser view port hovering over the news feed. You can try this manually too.

JAVA

driver.manage().window().setSize(new Dimension(1024, 500));

PYTHON

driver.set_window_size(300, 500)

The above code is in JAVA. But I believe that you can figure out the python equivalent.

NOTE : I have a screen resolution of 1366x768. So the value 500px might vary. But you certainly have to reduce the height.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
  • Okay, so Basically make the window size a bit smaller than the screen size? I'm using a virtual display (Xvfb), so I can adjust everything. – User Oct 25 '15 at 18:06
  • No need of adjusting the virtual display. Just resize the browser size (reduce the height). The Java Selenium code posted above does that. I will try and update the python code required. – StrikerVillain Oct 25 '15 at 18:09
  • Python code http://stackoverflow.com/questions/13571349/i-need-selenium-to-open-its-web-browser-in-a-larger-resolution-preferably-max http://stackoverflow.com/questions/15397483/how-do-i-set-browser-width-and-height-in-selenium-webdriver – StrikerVillain Oct 25 '15 at 18:13
  • I just noticed, I don't even set the window size. I only set the virtual display size. – User Oct 25 '15 at 18:14
  • By default the browser opens in a particular size even if you dont set it. You would have to override the default size – StrikerVillain Oct 25 '15 at 18:16
  • What about if I'm emulating mobile website versions with mobile screen sizes? I haven't checked if I have the problem there too. – User Oct 25 '15 at 18:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93307/discussion-between-striker-and-user). – StrikerVillain Oct 25 '15 at 18:18