|
| 1 | +# 에이전트를 활용한 웹 브라우저 자동화 🤖🌐[[web-browser-automation-with-agents-🤖🌐]] |
| 2 | + |
| 3 | +[[open-in-colab]] |
| 4 | + |
| 5 | +이 노트북에서는 **에이전트 기반 웹 브라우저 자동화 시스템**을 구축해보겠습니다! 이 시스템은 웹사이트 탐색, 요소 상호작용, 정보 자동 추출이 가능합니다. |
| 6 | + |
| 7 | +에이전트는 다음과 같은 기능을 수행할 수 있습니다. |
| 8 | + |
| 9 | +- [x] 웹 페이지 탐색 |
| 10 | +- [x] 요소 클릭 |
| 11 | +- [x] 페이지 내 검색 |
| 12 | +- [x] 팝업 및 모달 처리 |
| 13 | +- [x] 정보 추출 |
| 14 | + |
| 15 | +단계별로 이 시스템을 구축해보겠습니다! |
| 16 | + |
| 17 | +먼저 필요한 의존성을 설치하기 위해 다음을 실행하세요. |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install smolagents selenium helium pillow -q |
| 21 | +``` |
| 22 | + |
| 23 | +필요한 라이브러리를 가져오고 환경 변수를 설정해보겠습니다. |
| 24 | + |
| 25 | +```python |
| 26 | +from io import BytesIO |
| 27 | +from time import sleep |
| 28 | + |
| 29 | +import helium |
| 30 | +from dotenv import load_dotenv |
| 31 | +from PIL import Image |
| 32 | +from selenium import webdriver |
| 33 | +from selenium.webdriver.common.by import By |
| 34 | +from selenium.webdriver.common.keys import Keys |
| 35 | + |
| 36 | +from smolagents import CodeAgent, tool |
| 37 | +from smolagents.agents import ActionStep |
| 38 | + |
| 39 | +# 환경 변수를 불러옵니다. |
| 40 | +load_dotenv() |
| 41 | +``` |
| 42 | + |
| 43 | +이제 에이전트가 웹 페이지를 탐색하고 상호작용할 수 있도록 하는 핵심 브라우저 상호작용 도구들을 만들어보겠습니다. |
| 44 | + |
| 45 | +```python |
| 46 | +@tool |
| 47 | +def search_item_ctrl_f(text: str, nth_result: int = 1) -> str: |
| 48 | + """ |
| 49 | + 현재 페이지에서 Ctrl + F를 사용해 지정된 텍스트를 검색하고, n번째로 등장하는 위치로 이동합니다. |
| 50 | + 인자: |
| 51 | + text: 검색할 텍스트 |
| 52 | + nth_result: 이동할 n번째 검색 결과 (기본값: 1) |
| 53 | + """ |
| 54 | + elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{text}')]") |
| 55 | + if nth_result > len(elements): |
| 56 | + raise Exception(f"Match n°{nth_result} not found (only {len(elements)} matches found)") |
| 57 | + result = f"Found {len(elements)} matches for '{text}'." |
| 58 | + elem = elements[nth_result - 1] |
| 59 | + driver.execute_script("arguments[0].scrollIntoView(true);", elem) |
| 60 | + result += f"Focused on element {nth_result} of {len(elements)}" |
| 61 | + return result |
| 62 | + |
| 63 | +@tool |
| 64 | +def go_back() -> None: |
| 65 | + """이전 페이지로 돌아갑니다.""" |
| 66 | + driver.back() |
| 67 | + |
| 68 | +@tool |
| 69 | +def close_popups() -> str: |
| 70 | + """ |
| 71 | + Closes any visible modal or pop-up on the page. Use this to dismiss pop-up windows! |
| 72 | + This does not work on cookie consent banners. |
| 73 | + """ |
| 74 | + webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform() |
| 75 | +``` |
| 76 | + |
| 77 | +Chrome으로 브라우저를 설정하고 스크린샷 기능을 구성해보겠습니다. |
| 78 | + |
| 79 | +```python |
| 80 | +# Configure Chrome options |
| 81 | +chrome_options = webdriver.ChromeOptions() |
| 82 | +chrome_options.add_argument("--force-device-scale-factor=1") |
| 83 | +chrome_options.add_argument("--window-size=1000,1350") |
| 84 | +chrome_options.add_argument("--disable-pdf-viewer") |
| 85 | +chrome_options.add_argument("--window-position=0,0") |
| 86 | + |
| 87 | +# Initialize the browser |
| 88 | +driver = helium.start_chrome(headless=False, options=chrome_options) |
| 89 | + |
| 90 | +# Set up screenshot callback |
| 91 | +def save_screenshot(memory_step: ActionStep, agent: CodeAgent) -> None: |
| 92 | + sleep(1.0) # Let JavaScript animations happen before taking the screenshot |
| 93 | + driver = helium.get_driver() |
| 94 | + current_step = memory_step.step_number |
| 95 | + if driver is not None: |
| 96 | + for previous_memory_step in agent.memory.steps: # Remove previous screenshots for lean processing |
| 97 | + if isinstance(previous_memory_step, ActionStep) and previous_memory_step.step_number <= current_step - 2: |
| 98 | + previous_memory_step.observations_images = None |
| 99 | + png_bytes = driver.get_screenshot_as_png() |
| 100 | + image = Image.open(BytesIO(png_bytes)) |
| 101 | + print(f"Captured a browser screenshot: {image.size} pixels") |
| 102 | + memory_step.observations_images = [image.copy()] # Create a copy to ensure it persists |
| 103 | + |
| 104 | + # Update observations with current URL |
| 105 | + url_info = f"Current url: {driver.current_url}" |
| 106 | + memory_step.observations = ( |
| 107 | + url_info if memory_step.observations is None else memory_step.observations + "\n" + url_info |
| 108 | + ) |
| 109 | +``` |
| 110 | + |
| 111 | +이제 웹 자동화 에이전트를 만들어보겠습니다. |
| 112 | + |
| 113 | +```python |
| 114 | +from smolagents import InferenceClientModel |
| 115 | + |
| 116 | +# Initialize the model |
| 117 | +model_id = "Qwen/Qwen2-VL-72B-Instruct" # You can change this to your preferred VLM model |
| 118 | +model = InferenceClientModel(model_id=model_id) |
| 119 | + |
| 120 | +# Create the agent |
| 121 | +agent = CodeAgent( |
| 122 | + tools=[go_back, close_popups, search_item_ctrl_f], |
| 123 | + model=model, |
| 124 | + additional_authorized_imports=["helium"], |
| 125 | + step_callbacks=[save_screenshot], |
| 126 | + max_steps=20, |
| 127 | + verbosity_level=2, |
| 128 | +) |
| 129 | + |
| 130 | +# Import helium for the agent |
| 131 | +agent.python_executor("from helium import *", agent.state) |
| 132 | +``` |
| 133 | + |
| 134 | +에이전트가 웹 자동화를 위해 Helium을 사용하려면 지침이 필요합니다. 다음은 제공할 지침입니다. |
| 135 | + |
| 136 | +```python |
| 137 | +helium_instructions = """ |
| 138 | +You can use helium to access websites. Don't bother about the helium driver, it's already managed. |
| 139 | +We've already ran "from helium import *" |
| 140 | +Then you can go to pages! |
| 141 | +Code: |
| 142 | +```py |
| 143 | +go_to('github.com/trending') |
| 144 | +```<end_code> |
| 145 | +
|
| 146 | +You can directly click clickable elements by inputting the text that appears on them. |
| 147 | +Code: |
| 148 | +```py |
| 149 | +click("Top products") |
| 150 | +```<end_code> |
| 151 | +
|
| 152 | +If it's a link: |
| 153 | +Code: |
| 154 | +```py |
| 155 | +click(Link("Top products")) |
| 156 | +```<end_code> |
| 157 | +
|
| 158 | +If you try to interact with an element and it's not found, you'll get a LookupError. |
| 159 | +In general stop your action after each button click to see what happens on your screenshot. |
| 160 | +Never try to login in a page. |
| 161 | +
|
| 162 | +To scroll up or down, use scroll_down or scroll_up with as an argument the number of pixels to scroll from. |
| 163 | +Code: |
| 164 | +```py |
| 165 | +scroll_down(num_pixels=1200) # This will scroll one viewport down |
| 166 | +```<end_code> |
| 167 | +
|
| 168 | +When you have pop-ups with a cross icon to close, don't try to click the close icon by finding its element or targeting an 'X' element (this most often fails). |
| 169 | +Just use your built-in tool `close_popups` to close them: |
| 170 | +Code: |
| 171 | +```py |
| 172 | +close_popups() |
| 173 | +```<end_code> |
| 174 | +
|
| 175 | +You can use .exists() to check for the existence of an element. For example: |
| 176 | +Code: |
| 177 | +```py |
| 178 | +if Text('Accept cookies?').exists(): |
| 179 | + click('I accept') |
| 180 | +```<end_code> |
| 181 | +""" |
| 182 | +``` |
| 183 | + |
| 184 | +이제 작업과 함께 에이전트를 실행할 수 있습니다! Wikipedia에서 정보를 찾는 것을 시도해보겠습니다. |
| 185 | + |
| 186 | +```python |
| 187 | +search_request = """ |
| 188 | +Please navigate to https://en.wikipedia.org/wiki/Chicago and give me a sentence containing the word "1992" that mentions a construction accident. |
| 189 | +""" |
| 190 | + |
| 191 | +agent_output = agent.run(search_request + helium_instructions) |
| 192 | +print("Final output:") |
| 193 | +print(agent_output) |
| 194 | +``` |
| 195 | + |
| 196 | +요청을 수정하여 다른 작업을 실행할 수 있습니다. 예를 들어, 제가 얼마나 열심히 일해야 하는지 알아보는 작업입니다. |
| 197 | + |
| 198 | +```python |
| 199 | +github_request = """ |
| 200 | +I'm trying to find how hard I have to work to get a repo in github.com/trending. |
| 201 | +Can you navigate to the profile for the top author of the top trending repo, and give me their total number of commits over the last year? |
| 202 | +""" |
| 203 | + |
| 204 | +agent_output = agent.run(github_request + helium_instructions) |
| 205 | +print("Final output:") |
| 206 | +print(agent_output) |
| 207 | +``` |
| 208 | + |
| 209 | +이 시스템은 특히 다음과 같은 작업에 효과적입니다. |
| 210 | +- 웹사이트에서 데이터 추출 |
| 211 | +- 웹 리서치 자동화 |
| 212 | +- UI 테스트 및 검증 |
| 213 | +- 콘텐츠 모니터링 |
0 commit comments