mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Add visible_only and strict_mode options to ClickTool (#4088)
Partially addresses: https://github.com/hwchase17/langchain/issues/4066
This commit is contained in:
parent
b3988621c5
commit
3223a97dc6
@ -26,6 +26,18 @@ class ClickTool(BaseBrowserTool):
|
|||||||
description: str = "Click on an element with the given CSS selector"
|
description: str = "Click on an element with the given CSS selector"
|
||||||
args_schema: Type[BaseModel] = ClickToolInput
|
args_schema: Type[BaseModel] = ClickToolInput
|
||||||
|
|
||||||
|
visible_only: bool = True
|
||||||
|
"""Whether to consider only visible elements."""
|
||||||
|
playwright_strict: bool = False
|
||||||
|
"""Whether to employ Playwright's strict mode when clicking on elements."""
|
||||||
|
playwright_timeout: float = 1_000
|
||||||
|
"""Timeout (in ms) for Playwright to wait for element to be ready."""
|
||||||
|
|
||||||
|
def _selector_effective(self, selector: str) -> str:
|
||||||
|
if not self.visible_only:
|
||||||
|
return selector
|
||||||
|
return f"{selector} >> visible=1"
|
||||||
|
|
||||||
def _run(
|
def _run(
|
||||||
self,
|
self,
|
||||||
selector: str,
|
selector: str,
|
||||||
@ -36,11 +48,18 @@ class ClickTool(BaseBrowserTool):
|
|||||||
raise ValueError(f"Synchronous browser not provided to {self.name}")
|
raise ValueError(f"Synchronous browser not provided to {self.name}")
|
||||||
page = get_current_page(self.sync_browser)
|
page = get_current_page(self.sync_browser)
|
||||||
# Navigate to the desired webpage before using this tool
|
# Navigate to the desired webpage before using this tool
|
||||||
|
selector_effective = self._selector_effective(selector=selector)
|
||||||
|
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
page.click(selector)
|
page.click(
|
||||||
return f"Clicked element '{selector}'"
|
selector_effective,
|
||||||
except Exception as e:
|
strict=self.playwright_strict,
|
||||||
return f"Error '{e}'"
|
timeout=self.playwright_timeout,
|
||||||
|
)
|
||||||
|
except PlaywrightTimeoutError:
|
||||||
|
return f"Unable to click on element '{selector}'"
|
||||||
|
return f"Clicked element '{selector}'"
|
||||||
|
|
||||||
async def _arun(
|
async def _arun(
|
||||||
self,
|
self,
|
||||||
@ -52,8 +71,15 @@ class ClickTool(BaseBrowserTool):
|
|||||||
raise ValueError(f"Asynchronous browser not provided to {self.name}")
|
raise ValueError(f"Asynchronous browser not provided to {self.name}")
|
||||||
page = await aget_current_page(self.async_browser)
|
page = await aget_current_page(self.async_browser)
|
||||||
# Navigate to the desired webpage before using this tool
|
# Navigate to the desired webpage before using this tool
|
||||||
|
selector_effective = self._selector_effective(selector=selector)
|
||||||
|
from playwright.async_api import TimeoutError as PlaywrightTimeoutError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await page.click(selector)
|
await page.click(
|
||||||
return f"Clicked element '{selector}'"
|
selector_effective,
|
||||||
except Exception as e:
|
strict=self.playwright_strict,
|
||||||
return f"Error '{e}'"
|
timeout=self.playwright_timeout,
|
||||||
|
)
|
||||||
|
except PlaywrightTimeoutError:
|
||||||
|
return f"Unable to click on element '{selector}'"
|
||||||
|
return f"Clicked element '{selector}'"
|
||||||
|
Loading…
Reference in New Issue
Block a user