Search code examples
pythonubuntuseleniumrenderingpyvirtualdisplay

chromedriver not rendering html pages properly in selenium+pyvirtualdisplay in ubuntu server


I am trying to render HTML pages in Ubuntu server (AWS-EC2) by using selenium and chromedriver with pyvirtualdisplay. The HTML pages do not appear as they appear in my Windows PC. Attaching screenshots taken from the webdriver.


Image as rendered in Windows PC

This is the snapshot of HTML as rendered by **Windows**

Image as rendered in Ubuntu server with pyvirtualdisplay

This is the snapshot of HTML as rendered by **Ubuntu**


Basic configuration done while running my python code (snippets of the python code) -

display_width=1366
display_height=768

Resolution is same as my Windows PC.

UserAgent="Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"

This is a standard user agent.

def create_driver(executable_path,extension_path):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--user-agent='+UserAgent)
    driver = webdriver.Chrome(executable_path=executable_path,     chrome_options=chrome_options)
    driver.set_window_size(display_width,display_height
    driver.delete_all_cookies()
    return driver

This code creates driver

display = Display(visible=0, size=(display_width, display_height))
display.start()
driver = create_driver(executable_path,extension_path)
input_html_file='SAMPLE'
driver.get('file://///var/tmp/' + input_html_file)

I have a saved HTML file which I open in the driver - which is SAMPLE.

It is the same HTML that I opened in Windows. As you see in the screenshot images, the text "91 seller reviews" wraps to the new line. Since I am working on calculating height of this block and other areas, it affects my results.

The code works like a charm in Windows, giving correct result (height), but in Ubuntu since rendering itself is incorrect, so is the result.

This is what I have tried so far -

  1. Changing resolution of the display. Did not work. For absolutely any display size, the screenshot looks the same.
  2. Changing font, font size etc. Did not work. Every block in HTML has different css, so it is pain in the neck.
  3. Changing browser. Did not work, either in Firefox or Chrome

I am running out of ideas, it would be very helpful if somebody shared their ideas or experiences or some solution to this issue.

I understand it could be a graphics driver issue, Windows has one but Ubuntu server doesn't.

Please help. Thank you, in anticipation.

Edit: Image descriptions.


Solution

  • I could actually figure out the solution to this.

    Apparantly, Xvfb renders text using its own inventory of fonts. Now webpages have different fonts and Ubuntu/Xvfb will not necessarily have that font by default. So, all the text is rendered in Verdana font.

    All I did was to add fonts to the directory of fonts, and rebuild the font cache.

    This post helped.

    Will be helpful to anyone who faces this issue.