How to close all windows that Selenium opens?

Hey guys, I'm using Selenium RC to do some kind of test now. And the driver I use is python. But now I am faced with a problem, that is: every time Selenium RC is launched and the URL is opened, it opens 2 windows, one for logging and the other for displaying HTML content. But I cannot close them all in a script.

here is my script:

#!/usr/bin/env python
#-*-coding:utf-8-*-
from selenium import selenium

def main():
    sel = selenium('localhost', 4444, '*firefox', 'http://www.sina.com.cn/')
    sel.start()
    try:
        sel.open('http://www.sina.com.cn/')
    except Exception, e:
        print e
    else:
        print sel.get_title()
    sel.close()
    sel.stop()

if __name__ == '__main__':
    main()

It is very easy to understand. I really want to close all the windows that selenium opens. I tried close () and stop (), but they all do not work.

+3
source share
3 answers

, -. :

#!/usr/bin/python
import webbrowser
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Firefox()
print "Browser fired-up!"
driver.get("https://www.something.com/")
driver.implicitly_wait(5)

while True:

    try:
        playlink = driver.find_element_by_xpath("/html/body/div[2]/div[1]/div/a")
        playlink.click()
        time.sleep(3)
    except NoSuchElementException: 
        print "playlink Element not found "
    else:
        backbutton = driver.find_element_by_id("back-to-bing-text")
        backbutton.click()

    try:
        quizlink = driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]/a/span/span[1]")
        quizlink.click()
    except NoSuchElementException: 
        print "quiz1 Element not found "
    else:
        print "quiz1 clicked"

    driver.quit()   

"driver.close()" , , . "driver.quit()" - .

+5

. , firefox-bin, firefox. firefox-bin firefox, . stop() , .

, AutomatedTester

+1

I can suggest making a system command using python to close firefox windows

Bussiere

0
source

Source: https://habr.com/ru/post/1758170/


All Articles