I want to create a function that opens the browser, just to have it in a more compact way every time I need to call it, using Selenium Python.
This the code:
def prepare_website():
chrome_driver = ChromeDriverManager().install()
driver = Chrome(service=Service(chrome_driver))
driver.maximize_window()
driver.get(some_link)
prepare_website()
The problem is that after executing it closes the browser. How to keep it open instead, as it actually would do if it were not in a function?
#Luca Giovanni Voglino, you can try the following modified code. basically detach option will close the driver but keep the browser open:
def prepare_website():
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_driver = ChromeDriverManager().install()
driver = Chrome(options=chrome_options, service=Service(chrome_driver))
driver.maximize_window()
driver.get("http://www.google.com")
prepare_website()
you will need to add the following import:
from selenium.webdriver.chrome.options import Options
When I run chrome driver from selenium the browser opens in minimized windows. but I want it to open by default as maximized
You can either use
driver.maximize_window() or
chrome_options.add_argument("--start-maximized") which will maximize the browser when ever it opens.
The following code was taken from this link. https://pythonbasics.org/selenium-maximize/
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.maximize_window()
time.sleep(5)
driver.get("https://www.python.org")
This is one of the methods to do so.
I have the following code:
options = Options()
options = options.set_headless( headless=True)
class Sel_Driver():
def __init__(self):
self.driver = webdriver.Firefox(firefox_options=options)
I can then use self.driver.get(url) as part of a method to open urls I feed in. This works - I can feed in and open the URLs, but they don't in headless mode.
(I initially defined the driver as self.driver = webdriver.Firefox(firefox_options=Options().set_headless(headless=True) - but that didn't work, so I tried it as above).
What am I missing? I don't understand why the driver is able to open pages, but the options aren't enabled.
Please try following code :
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options)
This will work for you for sure. Try it.Please specify the path of the driver. It is for chrome change it to firefox.
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:\\Users\\Username\\Downloads\\chromedriver.exe")
print("Firefox Headless Browser Invoked")
driver.get('https://www.facebook.com/')
jks = driver.find_element_by_id("email").get_attribute("class")
print(jks)
driver.quit()
When I'm trying to open a web page, its opening in a new chrome window stripped of all the extensions and modules. I'm not able to emulate the certain behavior of the website using selenium chrome browser window but I'm able to do the same thing in a normal chrome window without any issues.
from selenium import webdriver
driver = webdriver.Chrome(r'C:\chromedriver.exe')
driver.get("remote_worksplace_link")
id_box = driver.find_element_by_id('Enter user name')
id_box.send_keys('123456')
pass_box = driver.find_element_by_id('passwd')
pass_box.send_keys('123abc')
login_button = driver.find_element_by_id('Log_On')
login_button.click()
driver.implicitly_wait(2)
launch_button = driver.find_element_by_class_name('storeapp-icon ui-sortable-handle')
launch_button.click()
driver.implicitly_wait(5)
driver.close()
all extentions has its .crx file just you need to add those path
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
driver.get("url")
driver.quit()
I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.
My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.
The second one, however, works only with headed Chrome. If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
With the headed Chrome, I have a complete HTML printed.
This is how I start my headless Chrome :
options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors")
options.add_argument("headless")
driver = webdriver.Chrome(chrome_options=options)
Again, note that this works in another of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.
Python : 3.6.1, Chrome : 60.0.3112.78 (64 bits), Selenium : 3.4.3
Any idea?
Thanks.
** EDIT: Here is the beginning of the code**
url = 'https://10.11.227.21/tmui/'
driver.get(url + "login.jsp")
html_source = driver.page_source
print(html_source)
blocStatus = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_element_located((By.ID, "username")))
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('actualLogin')
inputElement = driver.find_element_by_id("passwd")
inputElement.send_keys('actualPassword')
inputElement.submit()
I had a same experience like you, and solved it by using xvfb and pyvirtualdisplay.
I use chromedrive=v2.3.1, chrome-browser=v60 and Selenium=3.4.3
In Headless chrome, some of script seems not to work as expected.
Please refer to vpassapera's comment in https://gist.github.com/addyosmani/5336747.
How about try it like below,
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
# Do Not use headless chrome option
# options.add_argument('headless')
url = 'https://10.11.227.21/tmui/'
driver.get(url + "login.jsp")
html_source = driver.page_source
print(html_source)
blocStatus = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_element_located((By.ID, "username")))
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('actualLogin')
inputElement = driver.find_element_by_id("passwd")
inputElement.send_keys('actualPassword')
inputElement.submit()
display.stop()
xvfb is required to use "pyvortualdisplay"
$ sudo apt-get install -y xvfb
Headless Chrome does not support insecure certificates and hence, websites with insecure certificates does not open living it blank. You need to add capabilities as follow:
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['acceptSslCerts'] = True
capabilities['acceptInsecureCerts'] = True
driver = webdriver.Chrome(chrome_options = chrome_options,executable_path='your path',desired_capabilities=capabilities)
driver.get("yourWebsite")
This will do the work.
I had the same issue and setting the window size in conftest.py solved it.
My code snippet:
#pytest.fixture
def chrome_options(chrome_options, pytestconfig):
if pytestconfig.getoption('headless'):
chrome_options.add_argument('--headless')
chrome_options.add_argument("window-size=1920,1080")
else:
chrome_options.add_argument("start-maximized");
return chrome_options
Headless chrome may be faster on same machine than headed, try adding some wait before locating password element.
For some if you remove the below, it would work.
driver.fullscreen_window()
refer https://github.com/SeleniumHQ/selenium/issues/4477
add below code
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument("--window-size=1920,1080")
self.chrome_options.add_argument("--disable-extensions")
self.chrome_options.add_argument("--proxy-server='direct://'")
self.chrome_options.add_argument("--proxy-bypass-list=*")
self.chrome_options.add_argument("--start-maximized")
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument('--disable-dev-shm-usage')
self.chrome_options.add_argument('--no-sandbox')
self.chrome_options.add_argument('--ignore-certificate-errors')
self.browser = webdriver.Chrome(options=self.chrome_options)
For my situation, headless did not work because I was behind a proxy. Apparently, Chrome is able to use the system proxy, but headless does not use the system proxy.
I simply needed to provide the proxy, then headless (as well as Chrome) worked.
options.add_argument('--proxy-server=http://myproxy:port')