ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•
์‚ฌ์ดํŠธ์— ๋”ฐ๋ผ, ๋กœ๊ทธ์ธ์„ ํ•˜๊ธฐ ์œ„ํ•ด ํŒ์—… ์ฐฝ์—์„œ ๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์ผ€์ด์Šค๋“ค์ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์žฌ ๋“œ๋ผ์ด๋ฒ„์—์„œ ํŒ์—…์ฐฝ์œผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•๊ณผ ์ด๋ฅผ ์ ์šฉํ•˜๋ฉด, ์‰ฝ๊ฒŒ ์ปจํŠธ๋กคํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ตฌํ˜„ ํ•˜๊ธฐ

from selenium import webdriver


class DefaultTest:
    def __init__(self, address: str):
        self.options = webdriver.ChromeOptions()
        # skip ssl
        self.options.add_argument("--ignore-certificate-errors")
        self.driver = webdriver.Chrome(chrome_options=self.options)
        self.address = address

    def teardown(self):
        self.driver.quit()

    def change_window(self, target: str):
        if target == 'parent':
            # child window close
            self.driver.close()
            self.driver.switch_to.window(self.driver.window_handles[0])
        elif target == 'child':
            self.driver.switch_to.window(self.driver.window_handles[1])
        else:
            print("Wrong target!")

    def auto_test(self):
        self.driver.get(self.address)
        self.driver.set_window_size(974, 1040)


if __name__ == '__main__':
    test_address = "192.168.0.1"
    test = DefaultTest(test_address)
    test.auto_test()

 ์œ„์™€ ๊ฐ™์ด `chage_window`๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ํŒ์—… ์ฐฝ์„ ์‰ฝ๊ฒŒ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ…Œ์ŠคํŠธํ•˜๋Š” ํŽ˜์ด์ง€์— ์ ‘๊ทผ ํ›„ ํŒ์—… ์ฐฝ์ด ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, `change_window("child")`๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด ํ˜„์žฌ driver๊ฐ€ ํƒ์ƒ‰ํ•˜๋Š” ์œˆ๋„์šฐ๋Š” ํŒ์—… ์ฐฝ์ด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ, ํŒ์—… ์ฐฝ์ด ์‹คํ–‰๋œ๋‹ค๋ฉด `window_handles[1]`๊ณผ ๊ฐ™์ด ๋ฆฌ์ŠคํŠธ๋กœ driver๊ฐ€ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.

 

 ํŒ์—… ์ฐฝ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚œ๋‹ค๋ฉด, driver๊ฐ€ ํƒ์ƒ‰ํ•˜๋Š” ์œˆ๋„์šฐ๋ฅผ ๋ฉ”์ธ ์œˆ๋„์šฐ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ์ด๋Š” ์ฒ˜๋ฆฌํ•˜๊ณ ์ž ํ•˜๋Š” ๋กœ์ง์— ๋”ฐ๋ผ ์ƒ์ดํ•  ์ˆ˜ ์žˆ๋‹ค.  ์ฆ‰, ํŒ์—… ์ฐฝ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” `window_handles`์™€ `switch_to.window`๋ฅผ ์ ์ ˆํžˆ ํ™œ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ์ปจํŠธ๋กคํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ

์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰ ๋  ๊ฒฝ์šฐ ์›นํŽ˜์ด์ง€ ํ™”๋ฉด

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

class DefaultTest:
    def __init__(self, address: str):
        self.driver = webdriver.Chrome()
        self.address = address

    def teardown(self):
        self.driver.quit()

    def change_window(self, target: str):
        if target == 'parent':
            # child window close
            self.driver.close()
            self.driver.switch_to.window(self.driver.window_handles[0])
        elif target == 'child':
            self.driver.switch_to.window(self.driver.window_handles[1])
        else:
            print("Wrong target!")

    def auto_test(self):
        self.driver.get(self.address)
        self.driver.set_window_size(974, 1040)
        self.driver.find_element(By.XPATH, "/html/body/p/a").click()
        self.change_window("child")
        self.driver.find_element(By.XPATH, '/html/body/form/table/tbody/tr[6]/td[2]/input').click()
        # ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด 5์ดˆ๊ฐ„ sleep
        sleep(5)
        self.change_window("parent")


if __name__ == '__main__':
    test_address = "http://demo.guru99.com/popup.php"
    test = DefaultTest(test_address)
    test.auto_test()
    test.teardown()

 `http://demo.guru99.com/popup.php`์ด๋ผ๋Š” ์ƒ˜ํ”Œ ํŽ˜์ด์ง€๊ฐ€ ์žˆ๊ธธ๋ž˜, ์ด ํŽ˜์ด์ง€๋ฅผ ํ†ตํ•ด ํŒ์—… ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด์•˜๋‹ค. ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰์ด ๋œ๋‹ค๋ฉด, ํŒ์—… ์ฐฝ(์ƒˆ ์ฐฝ)์ด ์‹คํ–‰๋œ ํ›„์— ์œ„์™€ ๊ฐ™์ด `Submit`์„ ํด๋ฆญ ํ›„ ์‹คํ–‰ํ•œ ๋ชจ๋“  ์ฐฝ์„ ์ข…๋ฃŒํ•œ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€