简体   繁体   中英

How to start a Selenium Firefox web browser minimized?

My python script is in a loop and runs every half an hour, when ever the code runs it opens Firefox browser. So if I am working/ looking at any other screen all of a sudden the browser window pops up. I want my browser to either start minimized or disappear but my code should execute

while(True):
    driver = webdriver.Firefox()
    driver.get("http://iiitb.campusmetalink.com/cml/pages/setup/ModuleHome.jsf")
    driver.find_element_by_name("recqIDId:j_id15").send_keys("IIITB")
    driver.find_element_by_name("recqIDId:j_id17").send_keys("username")
    driver.find_element_by_name("recqIDId:j_id19").send_keys("password")
    driver.find_element_by_name("recqIDId:j_id25").click()
    WebDriverWait(driver, 100)
    driver.find_element_by_xpath('//*[@id="moduleForm:j_id40_body"]/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr[6]/td[2]/a').click()
    driver.find_element_by_xpath('//*[@id="scheduleForm:coltrm"]').send_keys("2016TERM2")
    driver.find_element_by_xpath('//*[@id="scheduleForm:search"]').click()
    elements = driver.find_elements_by_xpath('//*[@id="scheduleForm:svres:tb"]/tr')
    if( len(elements) > 6):
        os.system("C:/wamp64/bin/php/php5.6.16/php C:/wamp64/www/srishti/space_auth/Source/Source_code/public/mail_includes.php")
    print elements
    driver.close()
    time.sleep(1800)

While Selenium does provide an inbuilt function to maximize the window, unfortunately it doesn't provide the same functionality to minimize the window.

The below code will move the browser window out of sight, but I don't know if this will fulfil your requirements.

driver.set_window_position(0, 0)

Unfortunately selenium webdriver does not provide any built in function to minimize the window . However you can position your window out of sight by using below code. driver.manage().window().setPosition(new Point(-2000, 0));

As other people already said: selenium webdriver does not provide any built in function to minimize the window BUT there are few roundabouts way to do it. One of my favourite way is using win32gui and win32con:

import win32gui, win32con
win32gui.ShowWindow(win32gui.GetForegroundWindow(), win32con.SW_MINIMIZE)

The bad side of this code is, that the browser must be on the foreground.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM