File size: 14,869 Bytes
eefd46e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents.agent_toolkits import PlayWrightBrowserToolkit\n",
    "from langchain.tools.playwright.utils import (\n",
    "    create_async_playwright_browser,\n",
    "    create_sync_playwright_browser,# A synchronous browser is available, though it isn't compatible with jupyter.\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This import is required only for jupyter notebooks, since they have their own eventloop\n",
    "import nest_asyncio\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[ClickTool(name='click_element', description='Click on an element with the given CSS selector', args_schema=<class 'langchain.tools.playwright.click.ClickToolInput'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, sync_browser=None, async_browser=<Browser type=<BrowserType name=chromium executable_path=/Users/tutu/Library/Caches/ms-playwright/chromium-1060/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=113.0.5672.53>),\n",
       " NavigateTool(name='navigate_browser', description='Navigate a browser to the specified URL', args_schema=<class 'langchain.tools.playwright.navigate.NavigateToolInput'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, sync_browser=None, async_browser=<Browser type=<BrowserType name=chromium executable_path=/Users/tutu/Library/Caches/ms-playwright/chromium-1060/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=113.0.5672.53>),\n",
       " NavigateBackTool(name='previous_webpage', description='Navigate back to the previous page in the browser history', args_schema=<class 'pydantic.main.BaseModel'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, sync_browser=None, async_browser=<Browser type=<BrowserType name=chromium executable_path=/Users/tutu/Library/Caches/ms-playwright/chromium-1060/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=113.0.5672.53>),\n",
       " ExtractTextTool(name='extract_text', description='Extract all the text on the current webpage', args_schema=<class 'pydantic.main.BaseModel'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, sync_browser=None, async_browser=<Browser type=<BrowserType name=chromium executable_path=/Users/tutu/Library/Caches/ms-playwright/chromium-1060/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=113.0.5672.53>),\n",
       " ExtractHyperlinksTool(name='extract_hyperlinks', description='Extract all hyperlinks on the current webpage', args_schema=<class 'langchain.tools.playwright.extract_hyperlinks.ExtractHyperlinksToolInput'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, sync_browser=None, async_browser=<Browser type=<BrowserType name=chromium executable_path=/Users/tutu/Library/Caches/ms-playwright/chromium-1060/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=113.0.5672.53>),\n",
       " GetElementsTool(name='get_elements', description='Retrieve elements in the current web page matching the given CSS selector', args_schema=<class 'langchain.tools.playwright.get_elements.GetElementsToolInput'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, sync_browser=None, async_browser=<Browser type=<BrowserType name=chromium executable_path=/Users/tutu/Library/Caches/ms-playwright/chromium-1060/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=113.0.5672.53>),\n",
       " CurrentWebPageTool(name='current_webpage', description='Returns the URL of the current page', args_schema=<class 'pydantic.main.BaseModel'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, sync_browser=None, async_browser=<Browser type=<BrowserType name=chromium executable_path=/Users/tutu/Library/Caches/ms-playwright/chromium-1060/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=113.0.5672.53>)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "async_browser = create_async_playwright_browser()\n",
    "toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)\n",
    "tools = toolkit.get_tools()\n",
    "tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tools_by_name = {tool.name: tool for tool in tools}\n",
    "navigate_tool = tools_by_name[\"navigate_browser\"]\n",
    "get_elements_tool = tools_by_name[\"get_elements\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Navigating to https://www.cnn.com/world returned status code 200'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "await navigate_tool.arun({\"url\": \"https://www.cnn.com/world\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"innerText\": \"Three Islamic Jihad commanders among multiple dead in Israeli strikes on Gaza\"}, {\"innerText\": \"Canada to expel Chinese diplomat following allegations of foreign influence\"}, {\"innerText\": \"Buckingham Palace releases official coronation portraits\"}, {\"innerText\": \"Cold, hungry and determined: Families climb freight trains to reach US border\"}, {\"innerText\": \"Webb telescope spies evidence of hidden planets around nearby star\"}, {\"innerText\": \"Iran executes two people convicted of blasphemy\"}, {\"innerText\": \"Russian forces lash out indiscriminately as Ukraine increases military pressure on frontline towns\"}, {\"innerText\": \"EU cancels Israel reception to avoid far-right minister Ben Gvir\"}, {\"innerText\": \"A change in Turkish leadership might not spell the end of ties with Russia\"}, {\"innerText\": \"Prince Louis, 5, conducts first royal engagement\"}, {\"innerText\": \"More than 400 dead in flooding in Democratic Republic of Congo\"}, {\"innerText\": \"Prince William tells King Charles the late Queen Elizabeth would be \\\\u2018proud\\\\u2019\"}, {\"innerText\": \"UK police face backlash over handling of anti-monarchy protests\"}, {\"innerText\": \"Evacuations from Zaporizhzhia renew concerns for nuclear power plant safety\"}, {\"innerText\": \"Why has a state in India\\\\u2019s northeast exploded in ethnic unrest?\"}, {\"innerText\": \"Migrant trapped in web of barbed wires at US-Mexico border as end of Title 42 nears\"}, {\"innerText\": \"Russia evacuating civilians in occupied Zaporizhzhia region\"}, {\"innerText\": \"29,000 people evacuated from Alberta, Canada due to wildfires\"}, {\"innerText\": \"Ukrainians will \\\\u2018fight to the absolute end,\\\\u2019 says former U.S. governor\"}, {\"innerText\": \"King Charles III is crowned in once-in-a-generation ceremony\"}, {\"innerText\": \"Church of England changes wording of public declaration in King Charles III\\\\u2019s coronation, following backlash\"}, {\"innerText\": \"Russian military blogger injured in car explosion, state media reports\"}, {\"innerText\": \"Ukraine says it used US-made Patriot system to intercept Russian hypersonic missile\"}, {\"innerText\": \"Why is Camilla being crowned and will she be called \\\\u2018Queen\\\\u2019?\"}, {\"innerText\": \"The Coronation of King Charles: Order of Service\"}, {\"innerText\": \"Arab League readmits Syria after 11-year absence\"}, {\"innerText\": \"Arab nations set to rehabilitate Syria\\\\u2019s pariah president in defiance of US\"}, {\"innerText\": \"Arab League may have enough votes to bring Syria back into the fold\"}, {\"innerText\": \"Israel says killers of British-Israeli mother and daughters shot dead in Nablus\"}, {\"innerText\": \"Saudi Arabia wants to be a bigger player in the Middle East \\\\u2014 this time with diplomacy\"}, {\"innerText\": \"Israel military and Palestinians trade fire following hunger striker\\\\u2019s death\"}, {\"innerText\": \"In pictures: The coronation of King Charles III\"}, {\"innerText\": \"What it was like at the coronation celebrations\"}, {\"innerText\": \"The week in 34 photos\"}, {\"innerText\": \"In pictures: Britain\\\\u2019s King Charles III\"}, {\"innerText\": \"Nigerian senator jailed for nine years in UK organ harvesting plot\"}, {\"innerText\": \"South Africans call for UK to return diamonds set in crown jewels\"}, {\"innerText\": \"Sudan\\\\u2019s first professional stage actress killed in shelling in Khartoum\"}, {\"innerText\": \"Clemency pleas for Nigerian senator convicted for organ harvesting plot in UK\"}, {\"innerText\": \"Cuban police crack down on demonstrators protesting widespread shortages\"}, {\"innerText\": \"Fire kills at least 27 in Peruvian gold mine\"}, {\"innerText\": \"Gold mine fire in Peru kills 27, country\\\\u2019s worst in two decades\"}, {\"innerText\": \"Ecuador is in trouble and its president may pay the price\"}, {\"innerText\": \"Vietnam and Laos record hottest temperatures ever as heat wave grips Southeast Asia\"}, {\"innerText\": \"Rohingya refugees demand citizenship and security on first return to Myanmar\"}, {\"innerText\": \"Australian surfer faces possible flogging, jail after alleged naked, drunken rampage in Indonesia\"}, {\"innerText\": \"He smelled something strange in his Tibetan hotel room. It was a dead body under his bed\"}, {\"innerText\": \"Seoul: The city at the front line of modern culture\"}, {\"innerText\": \"Weird and wonderful trains that break the rules\"}, {\"innerText\": \"Italian food: Classic dishes everyone needs to try\"}, {\"innerText\": \"Nagoya shakes off its rep as Japan\\\\u2019s \\\\u2018most boring city\\\\u2019\"}, {\"innerText\": \"North America\\\\u2019s best bar for 2023 serves pizza cocktails\"}, {\"innerText\": \"She was a \\\\u2018supermodel\\\\u2019 of the Pre-Raphaelite period. Now art historians are course-correcting the short life of Elizabeth Siddal\"}, {\"innerText\": \"Meet Ramata-Toulaye Sy, the director competing for the Palme d\\\\u2019Or with her first film\"}, {\"innerText\": \"Kate and Charlotte\\\\u2019s regal twinning at the coronation\"}, {\"innerText\": \"The hidden meaning behind Camilla\\\\u2019s coronation outfit\"}, {\"innerText\": \"Fashion at the coronation: What the guests wore\"}, {\"innerText\": \"More than 29,000 people are evacuated from communities throughout Alberta as wildfires rage in Canada\"}, {\"innerText\": \"Severe storms threaten to bring more damage to the Ohio and Mississippi valleys today\"}, {\"innerText\": \"Almost 25,000 people in Western Canada evacuated as wildfires rage across region amid hot weather and high winds\"}, {\"innerText\": \"Historic May storm brings snow and record cold temperatures to the Great Lakes\"}, {\"innerText\": \"\\\\u2018Not my King\\\\u2019: Hear why these Brits protested the coronation\"}, {\"innerText\": \"Russia ramps up attacks in Ukraine ahead of WWII Victory Day\"}, {\"innerText\": \"Ukraine: Russia launches overnight missile, drone attack\"}, {\"innerText\": \"Arab League restores Syria\\\\u2019s membership after 11-year absence\"}, {\"innerText\": \"Coronation festivities continue with the \\\\u2018Big Lunch\\\\u2019 street parties\"}, {\"innerText\": \"Russia appears to send World War II-era tanks to fight in Ukraine\"}, {\"innerText\": \"China says relations with US on \\\\u2018cold ice,\\\\u2019 but stabilizing ties a \\\\u2018top priority\\\\u2019\"}, {\"innerText\": \"Drone footage shows devastation of flooding in DRC\"}, {\"innerText\": \"Russia is sending museum pieces into war, but experts say they may still be effective\"}, {\"innerText\": \"That massive blob of seaweed piling up on Florida beaches \\\\u2013 bad for tourists, good for wildlife\"}, {\"innerText\": \"Fighting rages in Sudan as mediators race to end conflict\"}, {\"innerText\": \"See which weapon alternative Russia is turning to amid looming counteroffensive\"}, {\"innerText\": \"World\\\\u2019s first 3D-printed mosque to be built in Dubai\"}, {\"innerText\": \"Yasmeen Lari, \\\\u2018starchitect\\\\u2019 turned social engineer, wins one of architecture\\\\u2019s most coveted prizes\"}, {\"innerText\": \"A massive, newly restored Frank Lloyd Wright mansion is up for sale\"}, {\"innerText\": \"Are these the most sustainable architectural projects in the world?\"}, {\"innerText\": \"Step inside a $72 million London townhouse in a converted army barracks\"}, {\"innerText\": \"Denver Nuggets star Nikola Joki\\\\u0107 scores 53, shoves Suns owner Mat Ishbia as Phoenix levels series\"}, {\"innerText\": \"Rickie Fowler picks up snake with golf club at Wells Fargo Championship\"}, {\"innerText\": \"James Harden fulfills promise to Michigan State shooting survivor; scores 42 points in 76ers\\\\u2019 Game 4 win over Celtics\"}, {\"innerText\": \"Andy Murray wins first title since 2019 with victory over Tommy Paul in Aix-en-Provence\"}, {\"innerText\": \"\\\\u2018You\\\\u2019re not alone\\\\u2019: CNN Heroes share advice for Mental Health Awareness Month\"}, {\"innerText\": \"These Ukrainian veterinarians are risking their lives to care for dogs and cats in the war zone\"}, {\"innerText\": \"This CNN Hero is recruiting recreational divers to help rebuild reefs in Florida one coral at a time\"}, {\"innerText\": \"This CNN Hero offers judgment-free veterinary care for the pets of those experiencing homelessness\"}]'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The browser is shared across tools, so the agent can interact in a stateful manner\n",
    "await get_elements_tool.arun({\"selector\": \".container__headline\", \"attributes\": [\"innerText\"]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "await navigate_tool.arun({\"url\": \"https://www.baidu.com\"})\n",
    "news = await get_elements_tool.arun({\"selector\": \".s-bottom-layer-content\", \"attributes\": [\"innerText\"]})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}