Need Help with python/ selenium & small size browser

007

Registered Member
Joined
Dec 21, 2007
Messages
63
Reaction score
2
Hi, I newbie in python and trying to make a simple bot, using python with selenium webdriver and I want to open browser in mobile size,
but I stuck in coding, I can not make it work so far, I use Brave browser, windows 7 64, python 3.8, selenium-4.1.

using python selenium with Brave browser normal size, works fine, but this time I try to work with smaller size browser, and I can not make it work.
Grateful for correcting my code, When I run it, nothing happen, no error either, I get this: "exited with code=0 in 0.985 seconds "
here is my code:

from selenium.webdriver.chrome.options import Options
from selenium import webdriver


def Data():
options = Options()
options.binary_location = "C:\\Program Files (x86)\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
driver = webdriver.Chrome(chrome_options=Options, executable_path="C:\\_ChromeDriver-97.0.4692.71\\chromedriver.exe",)
mobile_emulation = {
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/90.0.1025.166 Mobile Safari/535.19"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
options.add_argument("--log-level=3")

Mybot = webdriver.Chrome(chrome_options=Options)
Mybot.set_window_size(500, 950)
Mybot.get('https://www.bing.com')

Appreciate your help.
 
Is there anything else that you're leaving out? What you've posted here simply accesses the page but doesn't do anything with it, exit with code 0 would be the expected result.
 
There is really no wrong with your code is that your full code ? but try to apply this for device metrics
https://stackoverflow.com/a/63378120 or this https://stackoverflow.com/a/63798638

Good luck :)

Thanks for the reply and link for codes,
there are many codes available online for opening browser in smaller size, but all of them using Chrome, I have tested many with Chrome, it works, but when I change the driver path and Browser path (Brave) to it, it does not work,

Since using Chrome makes my computer very slow, there is no point using Chrome browser.
I want to use Brave browser, or Epic or Vivaldi, these 3 browser does not take much resources from computer, but Chrome and Firefox it just does not work properly, almost freezing.
 
Is there anything else that you're leaving out? What you've posted here simply accesses the page but doesn't do anything with it, exit with code 0 would be the expected result.

Thanks for the reply, you are right in the code there is not an action, but it is a step by step process, the first thing is to open the browser in mobile size when running the code, and it won't,
 
are you sure both chromedriver and brave browser versions match?
 
are you sure both chromedriver and brave browser versions match?
Thanks for the reply, Yes I am sure, the Chrome driver version matches the Brave browser. I have tested it, and it opens up the normal size.
 
I have tested some more codes, and it works as just to open smaller size, but I can not see why the above code works,
I am really confused over these Option, options, Options, option things.

here is the code that works,
when I run it, it opens the browser in a smaller size, actually, the bing website opens in 375 wide as it is in the code, but the browser size is maybe 600 pixels, so the right side is empty, kind of strange.
from selenium.webdriver.chrome.options import Options
from selenium import webdriver

brave_path = "C:\\Program Files (x86)\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
option = webdriver.ChromeOptions()
option.binary_location = brave_path


mobile_emulation = {
"deviceMetrics": { "width": 375, "height": 640, "pixelRatio": 3.0 },
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/90.0.1025.166 Mobile Safari/535.19"
}

option.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(
executable_path="C:\\_ChromeDriver-97.0.4692.71\\chromedriver.exe", options=option
)

url = "https://www.bing.com"
driver.get(url)
 
OK, eventually I made it work, here is the correct coding for anyone who might get into the same problem,
I gave a path to the chrome driver inside the code above as you see, also I gave the path to the Environment Variables area as well,
it is not needed, so I removed it from my code, also some parts of the code should remain outside of def area.

from selenium import webdriver
brave_path = "C:\\Program Files (x86)\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
options = webdriver.ChromeOptions()
options.binary_location = brave_path
bot = webdriver.Chrome(options=options)
def Data():
mobile_emulation = {
"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/90.0.1025.166 Mobile Safari/535.19" }

options.add_experimental_option("mobileEmulation", mobile_emulation)
options.add_argument("--log-level=3")
Mybot.set_window_size(500, 667)
Mybot.get("https://www.bing.com")
 
Screenshot_1.png


Hi everybody
I want help with selenium Brave Browser
I want to turn off Trackers & ads blocking
every time I call a new browser
but i tried to look in brave://flags/
but completely failed and couldn't turn off
hope someone can help
The default every time you call the browser is to turn on Trackers & ads blocking
Thanks All
 
Back
Top