i try to zoom or unzoom chrome's tab but when i try to run it headlees there is error
selenium.common.exceptions.JavascriptException: Message: javascript error: chrome is not defined
without headless working good , is there any idea?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chrome_options)
driver.get('http://google.com')
driver.get('chrome://settings/')
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.5);')
The problem is, that you can't set the zoom in headless mode.
Not working
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chrome_options)
driver.get('http://google.com')
driver.get('chrome://settings/')
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.5);')
Working
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chrome_options)
driver.get('http://google.com')
driver.get('chrome://settings/')
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.5);')
You see that you are not able to access the chrome element in headless mode.
Related
I tried to launch chrome with selenium. but as soon as the browser loads the urls, google chrome closes automatically. here is the code:
'''
from selenium import webdriver
url= 'https://www.gmail.com'
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)
'''
Because selenium4 no need to use driver.close()
method they automatically close the driver after execution. So options.add_experimental_option("detach", True) argument will help you to prevent to close immediately after once launch the url with google chrome
# selenium 4
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
#chrome to stay open
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
url= 'https://www.gmail.com'
driver.get(url)
time.sleep(2)
I had Selenium in an previous version, and installed Selenium 4.2.0, and my code is not working:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
servico = Service(ChromeDriverManager().install())
navegador = webdriver.Chrome(service=servico)
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
from selenium.common.exceptions.WebDriverException import NoSuchElementException
options = Options()
options.headless = False
navegador.get("https://receita.pr.gov.br/")
print ("Headless Chrome Initialized")
# entry with cpf
navegador.find_element(By.XPATH, '//*[#id="cpfusuario"]').send_keys("000000000")
I uninstalled Selenium and reinstalled, also reinstalled Chrome Driver, but I don't know why it isn't working.
There is no error message.
In your code, you initialized options after you already launched the driver. That should be done before you launch the driver so that you can use those options with any other argument that you want to pass in, such as the service:
driver = webdriver.Chrome(options=options)
# Or in the case of your example with translation:
navegador = webdriver.Chrome(service=servico, options=options)
I have using python selenium to do some test but I don't want it to open any browser, so how I can use a headless browser?? can you guys help me :((
Here is my code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('/Users/toanhac/Downloads/chromedriver')
url = 'https://mail.google.com/mail/u/0/#inbox'
driver.get(url)
driver.implicitly_wait(15)
driver.find_element_by_id("identifierId").send_keys(mail)
driver.find_element_by_id("identifierNext").click()
driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_id("passwordNext").click()
Just set headless to True:
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
Full code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome('/Users/toanhac/Downloads/chromedriver', options=options)
url = 'https://mail.google.com/mail/u/0/#inbox'
driver.get(url)
driver.implicitly_wait(15)
driver.find_element_by_id("identifierId").send_keys(mail)
driver.find_element_by_id("identifierNext").click()
driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_id("passwordNext").click()
This is how I set up proxy for the headless chrome driver:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
PROXY = "137.220.34.109:8080" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(options=chrome_options, executable_path=r"C:\Users\Ertan\Downloads\chromedriver.exe")
chrome.get("http://whatismyipaddress.com")
This is the code I use to run selenium with the headless driver:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.headless = True
browser = webdriver.Chrome(options=options, executable_path=r'C:\Users\Ertan\Downloads\chromedriver.exe')
browser.get("https://www.google.com")
time.sleep(4)
browser.get_screenshot_as_file("headless.png")
How do I change my code to run headless through a proxy? How to merge the Options and ChromeOptions simultaneously?
Using #DebanjanB's reply in How to make firefox headless programatically in Selenium with python?,
I'm trying to use his code and change it to use --screenshot argument, but it's not working.
This is my code
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument( "--screenshot test.jpg http://google.com/" )
driver = webdriver.Firefox( firefox_options=options )
driver.get('http://google.com/')
print driver.title
driver.quit()
sys.exit()
Can someone let me know please how to use --screenshot with Python and Firefox? Thanks
Nevermind, I found a way. there is a function driver.save_screenshot('test.png'). I kept the line from my question and commented it out.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import sys
options = Options()
options.add_argument( "--headless" )
# options.add_argument( "--screenshot test.jpg http://google.com/" )
driver = webdriver.Firefox( firefox_options=options )
driver.get('http://google.com/')
driver.save_screenshot('test.png')
print driver.title
print driver.current_url
driver.quit()
sys.exit()