Full screen Firefox Python Selenium - python

I'm trying to start a full screen page in Firefox with Selenium in Python 3. The page opening works fine, but when I send the F11 key to the browser (the Full Screen key), anything happens. Here is my code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
firefox = webdriver.Firefox()
firefox.get('http://localhost')
firefox.maximize_window()
body = firefox.find_element_by_tag_name('html')
body.send_keys(Keys.F11)
Does anyone know how to make my page start in full screen ? I know it's possible with Chrome, but it's harder with Firefox

This is what worked for me.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://localhost')
driver.find_element_by_xpath('/html/body').send_keys(Keys.F11)
Hope this helps
Just realized I am using Python 2.6 vs your 3. Sorry about that, but at least you know it will work on an older Python version

Selenium has a built-in method to make the window fullscreen: fullscreen_window()
Selenium API - fullscreen_window()
Invokes the window manager-specific ‘full screen’ operation
browser.get("https://www.screenku.com")
browser.fullscreen_window()

pip3 install selenium pyautogui
#!/usr/bin/env python3
import pyautogui
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("dom.webnotifications.enabled", False)
profile.set_preference("general.useragent.override", "Mozilla/5.0")
profile.update_preferences()
browser = webdriver.Firefox(firefox_profile=profile,executable_path = '/usr/local/bin/geckodriver')
browser.get("https://www.screenku.com")
pyautogui.press('f11')

Related

I tried to open the site with this code using selenium, but the site was closed as soon as it was opened

When I run this code, the page opens and closes. I can't reach the page. Everything is in the latest version. I am a Windows user.
I was trying to open Instagram page with this code.
enter image description here
I tried to open the instagram site with this code and the site was closed as soon as it was opened.
Im not familiar with selenium but I know for a fact that
get() just gets the HTML code of the website and does not display it
You have to add the below Chrome Option:
options.add_experimental_option("detach", True)
Full code:
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(chrom_driver_pat), options=options)
driver.get(<url>)
Next time, don't post the image, post the code and explain your issue clearly.
First of all, please, do not use images when your want a code review or help, it is more easier to help with raw code.
Selenium now uses Service method to run webdriver.
To run what you want, you need to fix some parts of your code, like this:
from selenium import webdriver
# in my case, i'm using selenium==4.5.0, for this version, is necessary to
# use selenium.webdriver.chrome.service to use driver path
from selenium.webdriver.chrome.service import Service as ChromeService
# For Windows path, use r"C:\..." when you will use \
# Remember to set the executable file too
chrome_driver_path = r"C:\pythondriver\chromedriver\chromedriver.exe"
# you can use "universal" path too, like linux, using / instead of \
# chrome_driver_path = "C:/pythondriver/chromedriver/chromedriver.exe"
# instead using:
# webdriver.Chrome()
# driver = webdriver.Chrome(chrome_driver_path)
# use this:
chrome_service = ChromeService(chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service)
url = "https://instagram.com"
driver.get(url)
I tested using the configurations below and worked for me:
Chromedriver - 108.0.5359.71
Chrome - 108.0.5359.125 - 64 bits
If you need more help using Chrome Service, try look the Selenium Docs

Prevent Safari from closing in Mac using Selenium Python?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
safari_option = Options()
safari_option.add_experimental_option("detach", True)
# Prevent closing browser after done execution
driver = webdriver.Safari(options=safari_option)
driver.get("https://www.google.com/?client=safari")
elem = driver.find_element(By.NAME, "q")
elem.clear()
elem.send_keys("test")
I want to prevent safari from closing after the webdriver has finished so that I can see the process of automating another website clearly one by one.
I tried using the safari_option.add_experimental_option("detach", True)
as has been done here
Python selenium keep browser open
for chrome. But I'm not quite sure why it did not work for Safari instead.Seems like a simple problem but couldn't find an answer for it on google or maybe Im blind. Im pretty new to Selenium btw.
Thanks

Python Selenium - Clearing chrome cache

I'm trying to clear the cache from the chrome driver with selenium.
The below code worked fine for a day, and now it has stopped working for some reason. It redirects to the ClearBrowserData url, but it does not press enter to run the Clear Data button.
Am I doing something wrong? I would appreciate some help on this.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = 'path'
browser = webdriver.Chrome(executable_path = chromedriver)
browser.get('chrome://settings/clearBrowserData')
browser.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
beacuse shadowRoot
<settings-clear-browsing-data-dialog>
#shadow-root (open)
</settings-clear-browsing-data-dialog>

Selenium: Follow adfly redirect

Im trying to make a bot that visits my adfly link using the chrome webdriver. Every time I try to use the code below though, the webdriver tells me that there were too many redirects and doesn't follow through. The code below is just being used for testing at the moment:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server="+"http://102.129.249.120:8080")
browser = webdriver.Chrome(options=options)
browser.get("http://raboninco.com/18Whc")
Image of error here
Okay so i figured it out. I can use tor with selenium to get access to adfly. Works great btw. Thanks for the help and time guys. If you want to see the code I used, here it is:
from selenium import webdriver
import os
os.popen(r'C:\Users\joldb\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server="+"socks5://127.0.0.1:9050")
browser = webdriver.Chrome(options=options)
browser.get("http://raboninco.com/18Whc")

Take screenshot with selenium in Fullscreen mode

I am taking a screenshot by using selenium with no display. It's working but it would be nice if I could take a screenshot of the Fullscreen Browser (without Firefox Toolbar and so on, just the website). I tried the above code which should perform a F11 press. The code runs with no error, however Fullscreen is not working, so I guess the F11 command is somehow not executed. My OS is ubuntu.
Can somebody tell me how to take the screenshot in selenium in Fullscreen mode?
#!/usr/bin/env python
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
display = Display(visible=0, size=(1920, 1080))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
ActionChains(browser).send_keys(Keys.F11).perform()
browser.save_screenshot('screenshot.png')
browser.quit()
display.stop()
just select one element on the page and send the keys ,
elem = driver.find_element_by_name("your_element")
elem.send_keys(Keys.F11)
make sure that element is loaded in DOM.It worked for me.

Categories