Home > front end >  How can i set device memory in selenium python?
How can i set device memory in selenium python?

Time:04-10

Was doing some tests and comparing them to a real browser vs selenium: https://bot.sannysoft.com/ One of the atrributes on my webdriver is CHR_MEMORY IS FALSE On a real browser it is present, anybody has any idea of how I can change that?

CHR_MEMORY  FAIL    
{}

CodePudding user response:

You need to specify navigator.deviceMemory and navigator.userAgent

Look at this example based on selenium

Also, here is my approach with Playwright:

import re

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()

    page.add_init_script("""
           Object.defineProperty(navigator, 'deviceMemory', {
                 get: () => 8
           });
           Object.defineProperty(navigator, 'userAgent', {
             get: () => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
           });
    """)

    page.goto('https://bot.sannysoft.com')
    html = page.content()

    status = 'failed'
    if match := re.search('CHR_MEMORY<\/td><td ', html):
        status = match.group(1)

    print(status)
    # Prints 'passed'

    browser.close()
  • Related