Hướng dẫn how do you close a window in python selenium? - làm thế nào để bạn đóng một cửa sổ trong python selen?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    driver.close()
    driver.quit()
    

    Trong khi thực hiện các công cụ với nhiều trình duyệt selenium với nhiều tab thường sẽ mở để đóng các phương thức

    Print
    
    1 và
    Print
    
    2 này được sử dụng. Phương thức
    Print
    
    1 được sử dụng để đóng cửa sổ trình duyệt hiện tại mà tập trung được đặt, mặt khác, phương thức
    Print
    
    2 về cơ bản gọi phương thức trình điều khiển. Đổi liên tiếp tất cả các cửa sổ trình duyệt và kết thúc phiên webDriver một cách khéo léo.

    Cú pháp:

    Tranh luận :

    Print
    
    1 method closes the current window.
    Print
    
    2 method quits the driver instance, closing every associated window, which is opened.

    Cả hai phương thức không có lập luận

    Hành động được thực hiện: ________ 21 Phương thức Đóng cửa sổ hiện tại .________ 22 Phương thức Thoát khỏi thể hiện trình điều khiển, đóng mọi cửa sổ liên quan, được mở. The webpage will loaded then closes due to close() method.

    Mã cho Phương thức Đóng ():

    Đầu ra: Trang web sẽ được tải sau đó đóng do phương thức đóng (). The webpage will be loaded then window get quit due to quit() method.

    Tôi có trang web mở cửa sổ trình duyệt mới khi nhấp. Tôi có thể nhận được 2 tay cầm tuy nhiên trình điều khiển.close () luôn đóng cửa sổ đầu tiên/chính.

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    

    Bên dưới mã trang web gọi cửa sổ mới

    Print
    

    Hành vi tương tự cho Firefox là tốt. Python 3.6.7

    Tôi đã googled cho câu trả lời, nhưng

    Print
    
    7 nên thường xuyên được đề cập không phù hợp với tôi. Cửa sổ Chrome mà bài kiểm tra đang chạy vẫn còn mở.

    def test_getResults(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.start()
        # do stuff
    
    def tearDown(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.close()
        sel.stop()
    

    Bất kỳ ý tưởng? Tôi đang sử dụng Selenium Server 2.8.0 với Python 2.6 và chủ yếu sử dụng Windows Chrome 14 để kiểm tra.

    Hỏi ngày 11 tháng 10 năm 2011 lúc 22:00Oct 11, 2011 at 22:00

    9

    Print
    
    8 và
    Print
    
    9 là hai phương pháp khác nhau để đóng phiên trình duyệt trong selenium webdriver.

    Print
    
    8 - Nó đóng cửa sổ trình duyệt mà tập trung được đặt.

    Print
    
    9 - Về cơ bản, nó gọi phương thức Driver.dispose, lần lượt đóng tất cả các cửa sổ trình duyệt và kết thúc phiên webDriver một cách duyên dáng.

    Bạn nên sử dụng

    Print
    
    9 bất cứ khi nào bạn muốn kết thúc chương trình. Nó sẽ đóng tất cả các cửa sổ trình duyệt đã mở và chấm dứt phiên webDriver. Nếu bạn không sử dụng
    Print
    
    9 vào cuối chương trình, phiên webDriver sẽ không đóng đúng và các tệp sẽ không bị xóa bộ nhớ. Điều này có thể dẫn đến lỗi rò rỉ bộ nhớ.

    Hướng dẫn how do you close a window in python selenium? - làm thế nào để bạn đóng một cửa sổ trong python selen?

    Đã trả lời ngày 18 tháng 8 năm 2016 lúc 12:34Aug 18, 2016 at 12:34

    Hướng dẫn how do you close a window in python selenium? - làm thế nào để bạn đóng một cửa sổ trong python selen?

    Sneha Singhsneha SinghSneha Singh

    5015 Huy hiệu bạc6 Huy hiệu Đồng5 silver badges6 bronze badges

    3

    Bạn thực sự tạo ra một phiên selen thứ hai trong chức năng reverdown () của bạn. Bạn cần đặt phiên được tạo trong Setup () vào một biến thể hiện, sau đó đóng phiên đó trong Teardown ().

    class TestFoo(unittest.TestCase):
        def setUp(self):
            self.selenium = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
            self.selenium.start()
    
        def tearDown(self):
            self.selenium.stop()
    
        def test_bar(self):
            self.selenium.open("/somepage")
            #and so forth
    

    Đã trả lời ngày 29 tháng 10 năm 2011 lúc 1:07Oct 29, 2011 at 1:07

    Tôi đã làm việc với trình điều khiển web trong cả Java và C# và tôi sử dụng

    Trong Java:

    WebDriver driver;      
    driver.quit();
    

    Trong C#:

    IWebDriver Driver;
    Driver.Quit();
    

    Đã trả lời ngày 8 tháng 11 năm 2013 lúc 23:02Nov 8, 2013 at 23:02

    user2062360user2062360user2062360

    3053 Huy hiệu bạc8 Huy hiệu Đồng3 silver badges8 bronze badges

    Trong Python, sử dụng Selenium WebDriver cho Chrome, tôi cần gọi

    def test_getResults(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.start()
        # do stuff
    
    def tearDown(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.close()
        sel.stop()
    
    4 trước
    Print
    
    1:

    from selenium import webdriver
    

    trong setup ():

    options = webdriver.chrome.options.Options()
    options.add_argument("--disable-extensions") # optional and off-topic, but it conveniently prevents the popup 'Disable developer mode extensions' 
    self.driver = webdriver.Chrome(chrome_options=options)
    

    Trong reverdown ():

    self.driver.stop_client()
    self.driver.close()
    

    Đã trả lời ngày 18 tháng 8 năm 2016 lúc 12:07Aug 18, 2016 at 12:07

    RemiremiRemi

    1312 Huy hiệu đồng2 bronze badges

    1

    WebDriver driver;      
    driver.quit();
    

    Trên đây sẽ đóng tất cả các kính gió trình duyệt mở.

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    1

    Điều này sẽ đóng cửa sổ trình duyệt hiện tại trong tiêu điểm.

    Kate Paulk ♦

    30.9k8 Huy hiệu vàng52 Huy hiệu bạc108 Huy hiệu Đồng8 gold badges52 silver badges108 bronze badges

    Đã trả lời ngày 14 tháng 5 năm 2015 lúc 9:59May 14, 2015 at 9:59

    Sử dụng Testng và Java. Giả sử phương pháp này được đặt trong một số lớp Basetest được kế thừa bởi lớp kiểm tra, vì vậy hãy thử điều này:
    Assume this method is located in some BaseTest class which is inherited by test class, so try this:

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    2

    Đã trả lời ngày 30 tháng 4 năm 2014 lúc 6:37Apr 30, 2014 at 6:37

    user1829657user1829657user1829657

    792 Huy hiệu vàng2 Huy hiệu bạc7 Huy hiệu đồng2 gold badges2 silver badges7 bronze badges

    2

    Bạn có thể sử dụng

    def test_getResults(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.start()
        # do stuff
    
    def tearDown(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.close()
        sel.stop()
    
    6 hoặc
    def test_getResults(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.start()
        # do stuff
    
    def tearDown(self):
        sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel.close()
        sel.stop()
    
    7.

    Sử dụng

    Print
    
    1 cho một trình duyệt để đóng và
    Print
    
    2 là đóng tất cả các trình duyệt bằng WebDriver. Nhưng tại sao
    Print
    
    1 không đóng cửa vào thời gian chạy, nhưng nó bị đóng cửa khi gỡ lỗi tôi không biết.

    Kenorb

    3671 Huy hiệu vàng3 Huy hiệu bạc15 Huy hiệu đồng1 gold badge3 silver badges15 bronze badges

    Đã trả lời ngày 4 tháng 6 năm 2014 lúc 9:41Jun 4, 2014 at 9:41

    1

    Đây là những gì làm việc cho tôi:

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    3

    Tôi có điều này trong các sự kiện trước khi xây dựng của mình trong Giải pháp Visual Studio. Điều này có thể được chạy từ dòng lệnh, chỉ cần gọi nó trong dự án của bạn và bạn nên được thiết lập.

    Tôi cũng có dòng sau để đóng trình điều khiển IE trong trường hợp không thực thi

    class TestFoo(unittest.TestCase):
        def setUp(self):
            self.selenium = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
            self.selenium.start()
    
        def tearDown(self):
            self.selenium.stop()
    
        def test_bar(self):
            self.selenium.open("/somepage")
            #and so forth
    
    1 hoặc
    class TestFoo(unittest.TestCase):
        def setUp(self):
            self.selenium = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
            self.selenium.start()
    
        def tearDown(self):
            self.selenium.stop()
    
        def test_bar(self):
            self.selenium.open("/somepage")
            #and so forth
    
    2

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    4

    Đã trả lời ngày 29 tháng 9 năm 2016 lúc 15:06Sep 29, 2016 at 15:06

    1

    Nếu bạn sử dụng chế độ không đầu, bạn vẫn thấy chúng không đóng, hãy thử giải pháp này 1) Nhận trình điều khiển làm người độc thân

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    5

    2) Sử dụng Đóng và Thoát trong Khối cuối cùng

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    6

    Kết quả: Bạn sẽ chỉ sử dụng một phiên bản tại thời điểm và nếu bạn thấy Trình quản lý tác vụ, bạn sẽ không tìm thấy sự treo máy của Chromedriver và Chrome.

    Đã trả lời ngày 5 tháng 2 năm 2019 lúc 21:46Feb 5, 2019 at 21:46

    Có thể giải pháp của tôi sẽ không phải là một giải pháp siêu thông minh, nhưng trước mỗi trường hợp kiểm tra tự động, tôi đã đặt:

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    7

    Tối đa hóa cửa sổ

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    8

    Và để đóng tất cả các cửa sổ của trường hợp thử nghiệm tôi sử dụng:

    from selenium import webdriver
    import time
    driver = webdriver.Chrome() 
    driver.get("file:///D:/blackhole/print.html")
    han = driver.window_handles
    print("handles:", han) # gets 1 handle
    time.sleep(2)
    click_btn = driver.find_element_by_link_text('Print')
    click_btn.click()
    han = driver.window_handles
    print("handles:", han) # gets 2 handles
    driver.switch_to_window = han[1] # first element is always first window handle
    driver.close() # main window close
    
    9

    Và điều này làm việc cho tôi trong Python.

    Hướng dẫn how do you close a window in python selenium? - làm thế nào để bạn đóng một cửa sổ trong python selen?

    Sophia

    3673 Huy hiệu bạc17 Huy hiệu đồng3 silver badges17 bronze badges

    Đã trả lời ngày 7 tháng 2 năm 2019 lúc 20:02Feb 7, 2019 at 20:02

    Điều này sẽ chỉ đóng cửa sổ, phiên selen của bạn vẫn tồn tại và bạn có thể thực hiện xử lý tiếp theo của mình bằng trình điều khiển selen.

    Print
    
    0

    Đã trả lời ngày 2 tháng 12 năm 2021 lúc 10:28Dec 2, 2021 at 10:28

    1

    Làm thế nào để bạn đóng một trang web trong Python?

    Đóng () Phương thức Đóng cửa sổ hiện tại. closes the current window.

    Làm thế nào để bạn đóng một tab trong Python?

    Phương thức đóng () được sử dụng. is used.

    Sự khác biệt giữa thoát () và đóng () là gì?

    thoát (): phương thức thoát () thoát khỏi trình điều khiển, đóng mọi cửa sổ liên quan.người lái xe.Đóng (): Phương thức đóng () Đóng cửa sổ hiện đang tập trung, bỏ trình điều khiển nếu cửa sổ hiện tại là cửa sổ mở duy nhất.Nếu không có Windows mở, nó sẽ lỗi.

    Làm cách nào để đóng một tab trong selen?

    Cách đóng tab trong selen.Trong mã bên dưới, trước tiên hãy lấy tất cả các tay cầm cửa sổ, sau đó chuyển sang tab để đóng.Sau đó, đóng trình điều khiển bằng trình điều khiển.close ().close the driver using driver. close().