繁体   English   中英

如何最小化启动Selenium Firefox Web浏览器?

[英]How to start a Selenium Firefox web browser minimized?

我的python脚本处于循环中,每半小时运行一次,只要代码运行,它将打开Firefox浏览器。 因此,如果我正在工作/正在查看其他屏幕,突然会弹出浏览器窗口。 我希望浏览器启动最小化或消失,但是我的代码应该执行

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)

尽管Selenium确实提供了使窗口最大化的内置功能,但不幸的是,它没有提供使窗口最小化的相同功能。

下面的代码将使浏览器窗口不可见,但是我不知道这是否可以满足您的要求。

driver.set_window_position(0, 0)

不幸的是,硒webdriver没有提供任何内置功能来最小化窗口。 但是,您可以使用以下代码将窗口置于看不见的位置。 driver.manage()。window()。setPosition(new Point(-2000,0));

正如其他人已经说过的:硒webdriver不提供任何内置功能来最小化窗口,但是很少有回旋的方式来做到这一点。 我最喜欢的方法之一是使用win32gui和win32con:

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

此代码的缺点是,浏览器必须位于前台。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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