import{s as _l,n as xl,o as Sl}from"../chunks/scheduler.00b97157.js";import{S as Rl,i as El,e as M,s as n,c as p,h as zl,a as o,d as t,b as a,f as Ql,g as i,j as y,k as rl,l as Yl,m as e,n as c,t as w,o as J,p as m}from"../chunks/index.a7c1a7a1.js";import{C as Hl,H as Al,E as Fl}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.4b5f77d2.js";import{C as u}from"../chunks/CodeBlock.7a3aa7b0.js";import{D as $l}from"../chunks/DocNotebookDropdown.be7b60e3.js";function ql(Tl){let U,A,Y,F,b,$,d,q,r,K,T,hl="在本notebook中，我们将创建一个<strong>基于Agent的网页浏览器自动化系统</strong>！该系统可以自动导航网站、与网页元素交互并提取信息。",L,h,Il="该Agent将能够：",D,j,Bl='<li class="task-list-item"><input type="checkbox" checked="" disabled=""/> 导航到网页</li> <li class="task-list-item"><input type="checkbox" checked="" disabled=""/> 点击元素</li> <li class="task-list-item"><input type="checkbox" checked="" disabled=""/> 在页面内搜索</li> <li class="task-list-item"><input type="checkbox" checked="" disabled=""/> 处理弹出窗口和模态框</li> <li class="task-list-item"><input type="checkbox" checked="" disabled=""/> 提取信息</li>',P,I,Cl="让我们一步步搭建这个系统！",O,B,Zl="首先运行以下命令安装所需依赖：",ll,C,sl,Z,Gl="让我们导入所需的库并设置环境变量：",tl,G,el,W,Wl="现在我们来创建核心的浏览器交互工具，使我们的Agent能够导航并与网页交互：",nl,V,al,v,Vl="让我们配置使用Chrome浏览器并设置截图功能：",Ml,f,ol,g,vl="现在我们来创建网页自动化Agent：",yl,X,pl,k,fl="Agent需要获得关于如何使用Helium进行网页自动化的指导。以下是我们将提供的操作说明：",il,N,cl,Q,gl="现在我们可以运行Agent执行任务了！让我们尝试在维基百科上查找信息：",wl,_,Jl,x,Xl="您可以通过修改请求参数执行不同任务。例如，以下请求可帮助我判断是否需要更加努力工作：",ml,S,Ul,R,kl="该系统在以下任务中尤为有效：",jl,E,Nl="<li>从网站提取数据</li> <li>网页研究自动化</li> <li>用户界面测试与验证</li> <li>内容监控</li>",ul,z,bl,H,dl;return b=new Hl({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),d=new $l({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/smolagents_doc/zh/web_browser.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/smolagents_doc/zh/pytorch/web_browser.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/smolagents_doc/zh/tensorflow/web_browser.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/smolagents_doc/zh/web_browser.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/smolagents_doc/zh/pytorch/web_browser.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/smolagents_doc/zh/tensorflow/web_browser.ipynb"}]}}),r=new Al({props:{title:"使用Agent实现网页浏览器自动化 🤖🌐",local:"使用agent实现网页浏览器自动化-",headingTag:"h1"}}),C=new u({props:{code:"cGlwJTIwaW5zdGFsbCUyMHNtb2xhZ2VudHMlMjBzZWxlbml1bSUyMGhlbGl1bSUyMHBpbGxvdyUyMC1x",highlighted:"pip install smolagents selenium helium pillow -q",wrap:!1}}),G=new u({props:{code:"ZnJvbSUyMGlvJTIwaW1wb3J0JTIwQnl0ZXNJTyUwQWZyb20lMjB0aW1lJTIwaW1wb3J0JTIwc2xlZXAlMEElMEFpbXBvcnQlMjBoZWxpdW0lMEFmcm9tJTIwZG90ZW52JTIwaW1wb3J0JTIwbG9hZF9kb3RlbnYlMEFmcm9tJTIwUElMJTIwaW1wb3J0JTIwSW1hZ2UlMEFmcm9tJTIwc2VsZW5pdW0lMjBpbXBvcnQlMjB3ZWJkcml2ZXIlMEFmcm9tJTIwc2VsZW5pdW0ud2ViZHJpdmVyLmNvbW1vbi5ieSUyMGltcG9ydCUyMEJ5JTBBZnJvbSUyMHNlbGVuaXVtLndlYmRyaXZlci5jb21tb24ua2V5cyUyMGltcG9ydCUyMEtleXMlMEElMEFmcm9tJTIwc21vbGFnZW50cyUyMGltcG9ydCUyMENvZGVBZ2VudCUyQyUyMHRvb2wlMEFmcm9tJTIwc21vbGFnZW50cy5hZ2VudHMlMjBpbXBvcnQlMjBBY3Rpb25TdGVwJTBBJTBBJTIzJTIwTG9hZCUyMGVudmlyb25tZW50JTIwdmFyaWFibGVzJTBBbG9hZF9kb3RlbnYoKQ==",highlighted:`<span class="hljs-keyword">from</span> io <span class="hljs-keyword">import</span> BytesIO
<span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep

<span class="hljs-keyword">import</span> helium
<span class="hljs-keyword">from</span> dotenv <span class="hljs-keyword">import</span> load_dotenv
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">from</span> selenium <span class="hljs-keyword">import</span> webdriver
<span class="hljs-keyword">from</span> selenium.webdriver.common.by <span class="hljs-keyword">import</span> By
<span class="hljs-keyword">from</span> selenium.webdriver.common.keys <span class="hljs-keyword">import</span> Keys

<span class="hljs-keyword">from</span> smolagents <span class="hljs-keyword">import</span> CodeAgent, tool
<span class="hljs-keyword">from</span> smolagents.agents <span class="hljs-keyword">import</span> ActionStep

<span class="hljs-comment"># Load environment variables</span>
load_dotenv()`,wrap:!1}}),V=new u({props:{code:"JTQwdG9vbCUwQWRlZiUyMHNlYXJjaF9pdGVtX2N0cmxfZih0ZXh0JTNBJTIwc3RyJTJDJTIwbnRoX3Jlc3VsdCUzQSUyMGludCUyMCUzRCUyMDEpJTIwLSUzRSUyMHN0ciUzQSUwQSUyMCUyMCUyMCUyMCUyMiUyMiUyMiUwQSUyMCUyMCUyMCUyMFNlYXJjaGVzJTIwZm9yJTIwdGV4dCUyMG9uJT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CUyMCUyMCUyMGRyaXZlci5leGVjdXRlX3NjcmlwdCglMjJhcmd1bWVudHMlNUIwJTVELnNjcm9sbEludG9WaWV3KHRydWUpJTNCJTIyJTJDJTIwZWxlbSklMEElMjAlMjAlMjAlMjByZXN1bHQlMjAlMkIlM0QlMjBmJTIyRm9jdXNlZCUyMG9uJTIwZWxlbWVudCUyMCU3Qm50aF9yZXN1bHQlN0QlMjBvZiUyMCU3QmxlbihlbGVtZW50cyklN0QlMjIlMEElMjAlMjAlMjAlMjByZXR1cm4lMjByZXN1bHQlMEElMEElNDB0b29sJTBBZGVmJTIwZ29fYmFjaygpJTIwLSUzRSUyME5vbmUlM0ElMEElMjAlMjAlMjAlMjAlMjIlMjIlMjJHb2VzJTIwYmFjayUyMHRvJTIwcHJldmlvdXMlMjBwYWdlLiUyMiUyMiUyMiUwQSUyMCUyMCUyMCUyMGRyaXZlci5iYWNrKCklMEElMEElNDB0b29sJTBBZGVmJTIwY2xvc2VfcG9wdXBzKCklMjAtJTNFJTIwc3RyJTNBJTBBJTIwJTIwJTIwJTIwJTIyJTIyJTIyJTBBJTIwJTIwJTIwJTIwQ2xvc2VzJTIwYW55JTIwdmlzaWJsZSUyMG1vZGFsJTIwb3IlMjBwb3AtdXAlMjBvbiUyMHRoZSUyMHBhZ2UuJTIwVXNlJTIwdGhpcyUyMHRvJTIwZGlzbWlzcyUyMHBvcC11cCUyMHdpbmRvd3MhJTBBJTIwJTIwJTIwJTIwVGhpcyUyMGRvZXMlMjBub3QlMjB3b3JrJTIwb24lMjBjb29raWUlMjBjb25zZW50JTIwYmFubmVycy4lMEElMjAlMjAlMjAlMjAlMjIlMjIlMjIlMEElMjAlMjAlMjAlMjB3ZWJkcml2ZXIuQWN0aW9uQ2hhaW5zKGRyaXZlcikuc2VuZF9rZXlzKEtleXMuRVNDQVBFKS5wZXJmb3JtKCk=",highlighted:`<span class="hljs-meta">@tool</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">search_item_ctrl_f</span>(<span class="hljs-params">text: <span class="hljs-built_in">str</span>, nth_result: <span class="hljs-built_in">int</span> = <span class="hljs-number">1</span></span>) -&gt; <span class="hljs-built_in">str</span>:
    <span class="hljs-string">&quot;&quot;&quot;
    Searches for text on the current page via Ctrl + F and jumps to the nth occurrence.
    Args:
        text: The text to search for
        nth_result: Which occurrence to jump to (default: 1)
    &quot;&quot;&quot;</span>
    elements = driver.find_elements(By.XPATH, <span class="hljs-string">f&quot;//*[contains(text(), &#x27;<span class="hljs-subst">{text}</span>&#x27;)]&quot;</span>)
    <span class="hljs-keyword">if</span> nth_result &gt; <span class="hljs-built_in">len</span>(elements):
        <span class="hljs-keyword">raise</span> Exception(<span class="hljs-string">f&quot;Match n°<span class="hljs-subst">{nth_result}</span> not found (only <span class="hljs-subst">{<span class="hljs-built_in">len</span>(elements)}</span> matches found)&quot;</span>)
    result = <span class="hljs-string">f&quot;Found <span class="hljs-subst">{<span class="hljs-built_in">len</span>(elements)}</span> matches for &#x27;<span class="hljs-subst">{text}</span>&#x27;.&quot;</span>
    elem = elements[nth_result - <span class="hljs-number">1</span>]
    driver.execute_script(<span class="hljs-string">&quot;arguments[0].scrollIntoView(true);&quot;</span>, elem)
    result += <span class="hljs-string">f&quot;Focused on element <span class="hljs-subst">{nth_result}</span> of <span class="hljs-subst">{<span class="hljs-built_in">len</span>(elements)}</span>&quot;</span>
    <span class="hljs-keyword">return</span> result

<span class="hljs-meta">@tool</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">go_back</span>() -&gt; <span class="hljs-literal">None</span>:
    <span class="hljs-string">&quot;&quot;&quot;Goes back to previous page.&quot;&quot;&quot;</span>
    driver.back()

<span class="hljs-meta">@tool</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">close_popups</span>() -&gt; <span class="hljs-built_in">str</span>:
    <span class="hljs-string">&quot;&quot;&quot;
    Closes any visible modal or pop-up on the page. Use this to dismiss pop-up windows!
    This does not work on cookie consent banners.
    &quot;&quot;&quot;</span>
    webdri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QSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG1lbW9yeV9zdGVwLm9ic2VydmF0aW9uc19pbWFnZXMlMjAlM0QlMjAlNUJpbWFnZS5jb3B5KCklNUQlMjAlMjAlMjMlMjBDcmVhdGUlMjBhJTIwY29weSUyMHRvJTIwZW5zdXJlJTIwaXQlMjBwZXJzaXN0cyUwQSUwQSUyMCUyMCUyMCUyMCUyMyUyMFVwZGF0ZSUyMG9ic2VydmF0aW9ucyUyMHdpdGglMjBjdXJyZW50JTIwVVJMJTBBJTIwJTIwJTIwJTIwdXJsX2luZm8lMjAlM0QlMjBmJTIyQ3VycmVudCUyMHVybCUzQSUyMCU3QmRyaXZlci5jdXJyZW50X3VybCU3RCUyMiUwQSUyMCUyMCUyMCUyMG1lbW9yeV9zdGVwLm9ic2VydmF0aW9ucyUyMCUzRCUyMCglMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB1cmxfaW5mbyUyMGlmJTIwbWVtb3J5X3N0ZXAub2JzZXJ2YXRpb25zJTIwaXMlMjBOb25lJTIwZWxzZSUyMG1lbW9yeV9zdGVwLm9ic2VydmF0aW9ucyUyMCUyQiUyMCUyMiU1Q24lMjIlMjAlMkIlMjB1cmxfaW5mbyUwQSUyMCUyMCUyMCUyMCk=",highlighted:`<span class="hljs-comment"># Configure Chrome options</span>
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(<span class="hljs-string">&quot;--force-device-scale-factor=1&quot;</span>)
chrome_options.add_argument(<span class="hljs-string">&quot;--window-size=1000,1350&quot;</span>)
chrome_options.add_argument(<span class="hljs-string">&quot;--disable-pdf-viewer&quot;</span>)
chrome_options.add_argument(<span class="hljs-string">&quot;--window-position=0,0&quot;</span>)

<span class="hljs-comment"># Initialize the browser</span>
driver = helium.start_chrome(headless=<span class="hljs-literal">False</span>, options=chrome_options)

<span class="hljs-comment"># Set up screenshot callback</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">save_screenshot</span>(<span class="hljs-params">memory_step: ActionStep, agent: CodeAgent</span>) -&gt; <span class="hljs-literal">None</span>:
    sleep(<span class="hljs-number">1.0</span>)  <span class="hljs-comment"># Let JavaScript animations happen before taking the screenshot</span>
    driver = helium.get_driver()
    current_step = memory_step.step_number
    <span class="hljs-keyword">if</span> driver <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
        <span class="hljs-keyword">for</span> previous_memory_step <span class="hljs-keyword">in</span> agent.memory.steps:  <span class="hljs-comment"># Remove previous screenshots for lean processing</span>
            <span class="hljs-keyword">if</span> <span class="hljs-built_in">isinstance</span>(previous_memory_step, ActionStep) <span class="hljs-keyword">and</span> previous_memory_step.step_number &lt;= current_step - <span class="hljs-number">2</span>:
                previous_memory_step.observations_images = <span class="hljs-literal">None</span>
        png_bytes = driver.get_screenshot_as_png()
        image = Image.<span class="hljs-built_in">open</span>(BytesIO(png_bytes))
        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Captured a browser screenshot: <span class="hljs-subst">{image.size}</span> pixels&quot;</span>)
        memory_step.observations_images = [image.copy()]  <span class="hljs-comment"># Create a copy to ensure it persists</span>

    <span class="hljs-comment"># Update observations with current URL</span>
    url_info = <span class="hljs-string">f&quot;Current url: <span class="hljs-subst">{driver.current_url}</span>&quot;</span>
    memory_step.observations = (
        url_info <span class="hljs-keyword">if</span> memory_step.observations <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">else</span> memory_step.observations + <span class="hljs-string">&quot;\\n&quot;</span> + url_info
    )`,wrap:!1}}),X=new u({props:{code:"ZnJvbSUyMHNtb2xhZ2VudHMlMjBpbXBvcnQlMjBJbmZlcmVuY2VDbGllbnRNb2RlbCUwQSUwQSUyMyUyMEluaXRpYWxpemUlMjB0aGUlMjBtb2RlbCUwQW1vZGVsX2lkJTIwJTNEJTIwJTIybWV0YS1sbGFtYSUyRkxsYW1hLTMuMy03MEItSW5zdHJ1Y3QlMjIlMjAlMjAlMjMlMjBZb3UlMjBjYW4lMjBjaGFuZ2UlMjB0aGlzJTIwdG8lMjB5b3VyJTIwcHJlZmVycmVkJTIwbW9kZWwlMEFtb2RlbCUyMCUzRCUyMEluZmVyZW5jZUNsaWVudE1vZGVsKG1vZGVsX2lkJTNEbW9kZWxfaWQpJTBBJTBBJTIzJTIwQ3JlYXRlJTIwdGhlJTIwYWdlbnQlMEFhZ2VudCUyMCUzRCUyMENvZGVBZ2VudCglMEElMjAlMjAlMjAlMjB0b29scyUzRCU1QmdvX2JhY2slMkMlMjBjbG9zZV9wb3B1cHMlMkMlMjBzZWFyY2hfaXRlbV9jdHJsX2YlNUQlMkMlMEElMjAlMjAlMjAlMjBtb2RlbCUzRG1vZGVsJTJDJTBBJTIwJTIwJTIwJTIwYWRkaXRpb25hbF9hdXRob3JpemVkX2ltcG9ydHMlM0QlNUIlMjJoZWxpdW0lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBzdGVwX2NhbGxiYWNrcyUzRCU1QnNhdmVfc2NyZWVuc2hvdCU1RCUyQyUwQSUyMCUyMCUyMCUyMG1heF9zdGVwcyUzRDIwJTJDJTBBJTIwJTIwJTIwJTIwdmVyYm9zaXR5X2xldmVsJTNEMiUyQyUwQSklMEElMEElMjMlMjBJbXBvcnQlMjBoZWxpdW0lMjBmb3IlMjB0aGUlMjBhZ2VudCUwQWFnZW50LnB5dGhvbl9leGVjdXRvciglMjJmcm9tJTIwaGVsaXVtJTIwaW1wb3J0JTIwKiUyMiUyQyUyMGFnZW50LnN0YXRlKQ==",highlighted:`<span class="hljs-keyword">from</span> smolagents <span class="hljs-keyword">import</span> InferenceClientModel

<span class="hljs-comment"># Initialize the model</span>
model_id = <span class="hljs-string">&quot;meta-llama/Llama-3.3-70B-Instruct&quot;</span>  <span class="hljs-comment"># You can change this to your preferred model</span>
model = InferenceClientModel(model_id=model_id)

<span class="hljs-comment"># Create the agent</span>
agent = CodeAgent(
    tools=[go_back, close_popups, search_item_ctrl_f],
    model=model,
    additional_authorized_imports=[<span class="hljs-string">&quot;helium&quot;</span>],
    step_callbacks=[save_screenshot],
    max_steps=<span class="hljs-number">20</span>,
    verbosity_level=<span class="hljs-number">2</span>,
)

<span class="hljs-comment"># Import helium for the agent</span>
agent.python_executor(<span class="hljs-string">&quot;from helium import *&quot;</span>, agent.state)`,wrap:!1}}),N=new u({props:{code:"aGVsaXVtX2luc3RydWN0aW9ucyUyMCUzRCUyMCUyMiUyMiUyMiUwQVlvdSUyMGNhbiUyMHVzZSUyMGhlbGl1bSUyMHRvJTIwYWNjZXNzJTIwd2Vic2l0ZXMuJTIwRG9uJ3QlMjBib3RoZXIlMjBhYm91dCUyMHRoZSUyMGhlbGl1bSUyMGRyaXZlciUyQyUyMGl0J3MlMjBhbHJlYWR5JTIwbWFuYWdlZC4lMEFXZSd2ZSUyMGFscmVhZHklMjByYW4lMjAlMjJmcm9tJTIwaGVsaXVtJTIwaW1wb3J0JTIwKiUyMiUwQVRoZW4lMjB5b3UlMjBjYW4lMjBnbyUyMHRvJTIwcGFnZXMhJTBBQ29kZSUzQSUwQSU2MCU2MCU2MHB5JTBBZ29fdG8oJ2dpdGh1Yi5jb20lMkZ0cmVuZGluZycpJTBBJTYwJTYwJTYwJTNDZW5kX2NvZGUlM0UlMEElMEFZb3UlMjBjYW4lMjBkaXJlY3RseSUyMGNsaWNrJTIwY2xpY2thYmxlJTIwZWxlbWVudHMlMjBieSUyMGlucHV0dGluZyUyMHRoZSUyMHRleHQlMjB0aGF0JTIwYXBwZWFycyUyMG9uJTIwdGhlbS4lMEFDb2RlJTNBJTBBJTYwJTYwJTYwcHklMEFjbGljayglMjJUb3AlMjBwcm9kdWN0cyUyMiklMEElNjAlNjAlNjAlM0NlbmRfY29kZSUzRSUwQSUwQUlmJTIwaXQncyUyMGElMjBsaW5rJTNBJTBBQ29kZSUzQSUwQSU2MCU2MCU2MHB5JTBBY2xpY2soTGluayglMjJUb3AlMjBwcm9kdWN0cyUyMikpJTBBJTYwJTYwJTYwJTNDZW5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c3QlMjBvZnRlbiUyMGZhaWxzKS4lMEFKdXN0JTIwdXNlJTIweW91ciUyMGJ1aWx0LWluJTIwdG9vbCUyMCU2MGNsb3NlX3BvcHVwcyU2MCUyMHRvJTIwY2xvc2UlMjB0aGVtJTNBJTBBQ29kZSUzQSUwQSU2MCU2MCU2MHB5JTBBY2xvc2VfcG9wdXBzKCklMEElNjAlNjAlNjAlM0NlbmRfY29kZSUzRSUwQSUwQVlvdSUyMGNhbiUyMHVzZSUyMC5leGlzdHMoKSUyMHRvJTIwY2hlY2slMjBmb3IlMjB0aGUlMjBleGlzdGVuY2UlMjBvZiUyMGFuJTIwZWxlbWVudC4lMjBGb3IlMjBleGFtcGxlJTNBJTBBQ29kZSUzQSUwQSU2MCU2MCU2MHB5JTBBaWYlMjBUZXh0KCdBY2NlcHQlMjBjb29raWVzJTNGJykuZXhpc3RzKCklM0ElMEElMjAlMjAlMjAlMjBjbGljaygnSSUyMGFjY2VwdCcpJTBBJTYwJTYwJTYwJTNDZW5kX2NvZGUlM0UlMEElMjIlMjIlMjI=",highlighted:`helium_instructions = <span class="hljs-string">&quot;&quot;&quot;
You can use helium to access websites. Don&#x27;t bother about the helium driver, it&#x27;s already managed.
We&#x27;ve already ran &quot;from helium import *&quot;
Then you can go to pages!
Code:
\`\`\`py
go_to(&#x27;github.com/trending&#x27;)
\`\`\`&lt;end_code&gt;

You can directly click clickable elements by inputting the text that appears on them.
Code:
\`\`\`py
click(&quot;Top products&quot;)
\`\`\`&lt;end_code&gt;

If it&#x27;s a link:
Code:
\`\`\`py
click(Link(&quot;Top products&quot;))
\`\`\`&lt;end_code&gt;

If you try to interact with an element and it&#x27;s not found, you&#x27;ll get a LookupError.
In general stop your action after each button click to see what happens on your screenshot.
Never try to login in a page.

To scroll up or down, use scroll_down or scroll_up with as an argument the number of pixels to scroll from.
Code:
\`\`\`py
scroll_down(num_pixels=1200) # This will scroll one viewport down
\`\`\`&lt;end_code&gt;

When you have pop-ups with a cross icon to close, don&#x27;t try to click the close icon by finding its element or targeting an &#x27;X&#x27; element (this most often fails).
Just use your built-in tool \`close_popups\` to close them:
Code:
\`\`\`py
close_popups()
\`\`\`&lt;end_code&gt;

You can use .exists() to check for the existence of an element. For example:
Code:
\`\`\`py
if Text(&#x27;Accept cookies?&#x27;).exists():
    click(&#x27;I accept&#x27;)
\`\`\`&lt;end_code&gt;
&quot;&quot;&quot;</span>`,wrap:!1}}),_=new u({props:{code:"c2VhcmNoX3JlcXVlc3QlMjAlM0QlMjAlMjIlMjIlMjIlMEFQbGVhc2UlMjBuYXZpZ2F0ZSUyMHRvJTIwaHR0cHMlM0ElMkYlMkZlbi53aWtpcGVkaWEub3JnJTJGd2lraSUyRkNoaWNhZ28lMjBhbmQlMjBnaXZlJTIwbWUlMjBhJTIwc2VudGVuY2UlMjBjb250YWluaW5nJTIwdGhlJTIwd29yZCUyMCUyMjE5OTIlMjIlMjB0aGF0JTIwbWVudGlvbnMlMjBhJTIwY29uc3RydWN0aW9uJTIwYWNjaWRlbnQuJTBBJTIyJTIyJTIyJTBBJTBBYWdlbnRfb3V0cHV0JTIwJTNEJTIwYWdlbnQucnVuKHNlYXJjaF9yZXF1ZXN0JTIwJTJCJTIwaGVsaXVtX2luc3RydWN0aW9ucyklMEFwcmludCglMjJGaW5hbCUyMG91dHB1dCUzQSUyMiklMEFwcmludChhZ2VudF9vdXRwdXQp",highlighted:`search_request = <span class="hljs-string">&quot;&quot;&quot;
Please navigate to https://en.wikipedia.org/wiki/Chicago and give me a sentence containing the word &quot;1992&quot; that mentions a construction accident.
&quot;&quot;&quot;</span>

agent_output = agent.run(search_request + helium_instructions)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Final output:&quot;</span>)
<span class="hljs-built_in">print</span>(agent_output)`,wrap:!1}}),S=new u({props:{code:"Z2l0aHViX3JlcXVlc3QlMjAlM0QlMjAlMjIlMjIlMjIlMEFJJ20lMjB0cnlpbmclMjB0byUyMGZpbmQlMjBob3clMjBoYXJkJTIwSSUyMGhhdmUlMjB0byUyMHdvcmslMjB0byUyMGdldCUyMGElMjByZXBvJTIwaW4lMjBnaXRodWIuY29tJTJGdHJlbmRpbmcuJTBBQ2FuJTIweW91JTIwbmF2aWdhdGUlMjB0byUyMHRoZSUyMHByb2ZpbGUlMjBmb3IlMjB0aGUlMjB0b3AlMjBhdXRob3IlMjBvZiUyMHRoZSUyMHRvcCUyMHRyZW5kaW5nJTIwcmVwbyUyQyUyMGFuZCUyMGdpdmUlMjBtZSUyMHRoZWlyJTIwdG90YWwlMjBudW1iZXIlMjBvZiUyMGNvbW1pdHMlMjBvdmVyJTIwdGhlJTIwbGFzdCUyMHllYXIlM0YlMEElMjIlMjIlMjIlMEElMEFhZ2VudF9vdXRwdXQlMjAlM0QlMjBhZ2VudC5ydW4oZ2l0aHViX3JlcXVlc3QlMjAlMkIlMjBoZWxpdW1faW5zdHJ1Y3Rpb25zKSUwQXByaW50KCUyMkZpbmFsJTIwb3V0cHV0JTNBJTIyKSUwQXByaW50KGFnZW50X291dHB1dCk=",highlighted:`github_request = <span class="hljs-string">&quot;&quot;&quot;
I&#x27;m trying to find how hard I have to work to get a repo in github.com/trending.
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?
&quot;&quot;&quot;</span>

agent_output = agent.run(github_request + helium_instructions)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Final output:&quot;</span>)
<span class="hljs-built_in">print</span>(agent_output)`,wrap:!1}}),z=new Fl({props:{source:"https://github.com/huggingface/smolagents/blob/main/docs/source/zh/examples/web_browser.md"}}),{c(){U=M("meta"),A=n(),Y=M("p"),F=n(),p(b.$$.fragment),$=n(),p(d.$$.fragment),q=n(),p(r.$$.fragment),K=n(),T=M("p"),T.innerHTML=hl,L=n(),h=M("p"),h.textContent=Il,D=n(),j=M("ul"),j.innerHTML=Bl,P=n(),I=M("p"),I.textContent=Cl,O=n(),B=M("p"),B.textContent=Zl,ll=n(),p(C.$$.fragment),sl=n(),Z=M("p"),Z.textContent=Gl,tl=n(),p(G.$$.fragment),el=n(),W=M("p"),W.textContent=Wl,nl=n(),p(V.$$.fragment),al=n(),v=M("p"),v.textContent=Vl,Ml=n(),p(f.$$.fragment),ol=n(),g=M("p"),g.textContent=vl,yl=n(),p(X.$$.fragment),pl=n(),k=M("p"),k.textContent=fl,il=n(),p(N.$$.fragment),cl=n(),Q=M("p"),Q.textContent=gl,wl=n(),p(_.$$.fragment),Jl=n(),x=M("p"),x.textContent=Xl,ml=n(),p(S.$$.fragment),Ul=n(),R=M("p"),R.textContent=kl,jl=n(),E=M("ul"),E.innerHTML=Nl,ul=n(),p(z.$$.fragment),bl=n(),H=M("p"),this.h()},l(l){const s=zl("svelte-u9bgzb",document.head);U=o(s,"META",{name:!0,content:!0}),s.forEach(t),A=a(l),Y=o(l,"P",{}),Ql(Y).forEach(t),F=a(l),i(b.$$.fragment,l),$=a(l),i(d.$$.fragment,l),q=a(l),i(r.$$.fragment,l),K=a(l),T=o(l,"P",{"data-svelte-h":!0}),y(T)!=="svelte-qnpzrx"&&(T.innerHTML=hl),L=a(l),h=o(l,"P",{"data-svelte-h":!0}),y(h)!=="svelte-zjngas"&&(h.textContent=Il),D=a(l),j=o(l,"UL",{class:!0,"data-svelte-h":!0}),y(j)!=="svelte-179l98a"&&(j.innerHTML=Bl),P=a(l),I=o(l,"P",{"data-svelte-h":!0}),y(I)!=="svelte-12hfun9"&&(I.textContent=Cl),O=a(l),B=o(l,"P",{"data-svelte-h":!0}),y(B)!=="svelte-dm35fw"&&(B.textContent=Zl),ll=a(l),i(C.$$.fragment,l),sl=a(l),Z=o(l,"P",{"data-svelte-h":!0}),y(Z)!=="svelte-10w15qh"&&(Z.textContent=Gl),tl=a(l),i(G.$$.fragment,l),el=a(l),W=o(l,"P",{"data-svelte-h":!0}),y(W)!=="svelte-1tzm5xm"&&(W.textContent=Wl),nl=a(l),i(V.$$.fragment,l),al=a(l),v=o(l,"P",{"data-svelte-h":!0}),y(v)!=="svelte-1qf76sz"&&(v.textContent=Vl),Ml=a(l),i(f.$$.fragment,l),ol=a(l),g=o(l,"P",{"data-svelte-h":!0}),y(g)!=="svelte-czbgn0"&&(g.textContent=vl),yl=a(l),i(X.$$.fragment,l),pl=a(l),k=o(l,"P",{"data-svelte-h":!0}),y(k)!=="svelte-1lifcrx"&&(k.textContent=fl),il=a(l),i(N.$$.fragment,l),cl=a(l),Q=o(l,"P",{"data-svelte-h":!0}),y(Q)!=="svelte-1gzuxti"&&(Q.textContent=gl),wl=a(l),i(_.$$.fragment,l),Jl=a(l),x=o(l,"P",{"data-svelte-h":!0}),y(x)!=="svelte-1j23cdx"&&(x.textContent=Xl),ml=a(l),i(S.$$.fragment,l),Ul=a(l),R=o(l,"P",{"data-svelte-h":!0}),y(R)!=="svelte-12ic3i3"&&(R.textContent=kl),jl=a(l),E=o(l,"UL",{"data-svelte-h":!0}),y(E)!=="svelte-1edil9s"&&(E.innerHTML=Nl),ul=a(l),i(z.$$.fragment,l),bl=a(l),H=o(l,"P",{}),Ql(H).forEach(t),this.h()},h(){rl(U,"name","hf:doc:metadata"),rl(U,"content",Kl),rl(j,"class","contains-task-list")},m(l,s){Yl(document.head,U),e(l,A,s),e(l,Y,s),e(l,F,s),c(b,l,s),e(l,$,s),c(d,l,s),e(l,q,s),c(r,l,s),e(l,K,s),e(l,T,s),e(l,L,s),e(l,h,s),e(l,D,s),e(l,j,s),e(l,P,s),e(l,I,s),e(l,O,s),e(l,B,s),e(l,ll,s),c(C,l,s),e(l,sl,s),e(l,Z,s),e(l,tl,s),c(G,l,s),e(l,el,s),e(l,W,s),e(l,nl,s),c(V,l,s),e(l,al,s),e(l,v,s),e(l,Ml,s),c(f,l,s),e(l,ol,s),e(l,g,s),e(l,yl,s),c(X,l,s),e(l,pl,s),e(l,k,s),e(l,il,s),c(N,l,s),e(l,cl,s),e(l,Q,s),e(l,wl,s),c(_,l,s),e(l,Jl,s),e(l,x,s),e(l,ml,s),c(S,l,s),e(l,Ul,s),e(l,R,s),e(l,jl,s),e(l,E,s),e(l,ul,s),c(z,l,s),e(l,bl,s),e(l,H,s),dl=!0},p:xl,i(l){dl||(w(b.$$.fragment,l),w(d.$$.fragment,l),w(r.$$.fragment,l),w(C.$$.fragment,l),w(G.$$.fragment,l),w(V.$$.fragment,l),w(f.$$.fragment,l),w(X.$$.fragment,l),w(N.$$.fragment,l),w(_.$$.fragment,l),w(S.$$.fragment,l),w(z.$$.fragment,l),dl=!0)},o(l){J(b.$$.fragment,l),J(d.$$.fragment,l),J(r.$$.fragment,l),J(C.$$.fragment,l),J(G.$$.fragment,l),J(V.$$.fragment,l),J(f.$$.fragment,l),J(X.$$.fragment,l),J(N.$$.fragment,l),J(_.$$.fragment,l),J(S.$$.fragment,l),J(z.$$.fragment,l),dl=!1},d(l){l&&(t(A),t(Y),t(F),t($),t(q),t(K),t(T),t(L),t(h),t(D),t(j),t(P),t(I),t(O),t(B),t(ll),t(sl),t(Z),t(tl),t(el),t(W),t(nl),t(al),t(v),t(Ml),t(ol),t(g),t(yl),t(pl),t(k),t(il),t(cl),t(Q),t(wl),t(Jl),t(x),t(ml),t(Ul),t(R),t(jl),t(E),t(ul),t(bl),t(H)),t(U),m(b,l),m(d,l),m(r,l),m(C,l),m(G,l),m(V,l),m(f,l),m(X,l),m(N,l),m(_,l),m(S,l),m(z,l)}}}const Kl='{"title":"使用Agent实现网页浏览器自动化 🤖🌐","local":"使用agent实现网页浏览器自动化-","sections":[],"depth":1}';function Ll(Tl){return Sl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ts extends Rl{constructor(U){super(),El(this,U,Ll,ql,_l,{})}}export{ts as component};
