kaliiiiiiiiii / Selenium-Profiles

undetected Selenium using chromedriver and emulation / device profiles
Other
273 stars 29 forks source link

window.screen width and height values aren't changed to the profile's emulation values #29

Closed nradosevic closed 1 year ago

nradosevic commented 1 year ago

Describe the bug window.screen width and height values aren't changed to the profile's emulation values. Might actually be undetected chromedriver's fault because it doesn't support mobileEmulation experimental option, but anyway would be really meaningful to find a workaround

To Reproduce Run the code from the example, using any profile (Desktop or Android) and check the window.screen values in dev tools

Expected behavior or error-message window.screen width and height values should get the value

Environment (please complete the following information):

nradosevic commented 1 year ago

Actually, it seems that this line return self.driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', emulation) should do it, for some reason it doesn't

nradosevic commented 1 year ago

When I execute this directly on the driver instance after mydriver.start finishes it changes those values

driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride", {
    "deviceScaleFactor": 3,
    "width": 390,
    "height": 844,
    "screenWidth": 390,
    "screenHeight": 844,
    "mobile": True
})
kaliiiiiiiiii commented 1 year ago

Actually, it seems that this line return self.driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', emulation) should do it, for some reason it doesn't

Wonder why it doesn't work. Stack trace should be driver.py==>scripts/profiles.py==>scripts/cdp_tools.py

Noticed some time ago, that after initializing the driver, it needs to wait some time bevore executing all the cdp_cmd commands. Solved that in driver.py by just getting some url.

EDIT: Couldn't reproduce the issue. Even when using uc_driver=True and profiles.Android, the window.screen size properties in javascript are correct in my case: image

@nradosevic Or do you mean, that the window on your Desktop is to big? I that case, yes, that's a bug, undetected-chromedriver#1100 in undetected-chromedriver at undetected_chromedriver/init.py#L379, because it adds some arguments by default.

nradosevic commented 1 year ago

I am puzzled how window.screen values are correct in your case. In my case I found a problem. Problem is that the emulation object in the default profiles is lacking those options: screenWidth, screenHeight, because they are needed to set the correct screen size when using driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride"

After adding those to the default profiles, it works for me too

kaliiiiiiiiii commented 1 year ago

I am puzzled how window.screen values are correct in your case. In my case I found a problem. Problem is that the emulation object in the default profiles is lacking those options: screenWidth, screenHeight, because they are needed to set the correct screen size when using driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride"

After adding those to the default profiles, it works for me too

Oh I see In that case, I'll implement following in some time:

Also, this likely doesn't have anything to do with with undetected-chromedriver. I assuem it's some inconsistency in in the chrome-developer-protocoll implementation of chromedriver nur or Chromium

AlexPaiva commented 1 year ago

I am puzzled how window.screen values are correct in your case. In my case I found a problem. Problem is that the emulation object in the default profiles is lacking those options: screenWidth, screenHeight, because they are needed to set the correct screen size when using driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride" After adding those to the default profiles, it works for me too

Oh I see In that case, I'll implement following in some time:

* If the keys don't exist in `profile`, copy fom `height` and `with`

Also, this likely doesn't have anything to do with with undetected-chromedriver. I assuem it's some inconsistency in in the chrome-developer-protocoll implementation of chromedriver nur or Chromium

Not sure because I remember on UC discussions people complaining regarding this, but might be as you said so both packages suffer from this

kaliiiiiiiiii commented 1 year ago

I am puzzled how window.screen values are correct in your case. In my case I found a problem. Problem is that the emulation object in the default profiles is lacking those options: screenWidth, screenHeight, because they are needed to set the correct screen size when using driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride" After adding those to the default profiles, it works for me too

Oh I see In that case, I'll implement following in some time:

  • If the keys don't exist in profile, copy fom height and with

Also, this likely doesn't have anything to do with with undetected-chromedriver. I assuem it's some inconsistency in in the chrome-developer-protocoll implementation of chromedriver nur or Chromium

Implemented with https://github.com/kaliiiiiiiiii/Selenium-Profiles/commit/6f259906c6e8035d93753c25622cfb0831d5f272