ํฐ์คํ ๋ฆฌ ๋ทฐ
Selenium: ํ์ ์ฐฝ ์ปจํธ๋กคํ๊ธฐ
dirmathfl 2020. 12. 20. 16:25์ฌ์ดํธ์ ๋ฐ๋ผ, ๋ก๊ทธ์ธ์ ํ๊ธฐ ์ํด ํ์ ์ฐฝ์์ ๋ก๊ทธ์ธ ์ ๋ณด๋ฅผ ์ ๋ ฅํ๋ ์ผ์ด์ค๋ค์ด ์๋ค. ๋ฐ๋ผ์ ํ์ฌ ๋๋ผ์ด๋ฒ์์ ํ์ ์ฐฝ์ผ๋ก ์ ๊ทผํ๊ธฐ ์ํ ๋ฐฉ๋ฒ๊ณผ ์ด๋ฅผ ์ ์ฉํ๋ฉด, ์ฝ๊ฒ ์ปจํธ๋กคํ ์ ์๋ค.
๊ตฌํ ํ๊ธฐ
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`์ ํด๋ฆญ ํ ์คํํ ๋ชจ๋ ์ฐฝ์ ์ข ๋ฃํ๋ค.
'๐ต๏ธโโ๏ธ ์ฐ๋ฌผ ๋ฐ์ ๊ฐ๊ตฌ๋ฆฌ > Selenium' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Selenium : 'ํน์ ํ๋ก๊ทธ๋จ'์(๋ฅผ) ์ฌ์๊ฒ ์ต๋๊น? (0) | 2022.01.11 |
---|---|
Selenium: webdriver_manager ํ์ฉํ๊ธฐ (0) | 2022.01.10 |
Selenium: ์์ ํ์ง ์์ ํ์ด์ง ์คํตํ๊ธฐ (8) | 2020.12.19 |
Selenium: Python์ผ๋ก ์์ํ๊ธฐ (4) | 2020.12.18 |