Spaces:
No application file
No application file
Create client_example.py
Browse files- client_example.py +154 -0
client_example.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import base64
|
| 3 |
+
import json
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
class BrowserAutomationClient:
|
| 7 |
+
def __init__(self, base_url: str):
|
| 8 |
+
self.base_url = base_url.rstrip('/')
|
| 9 |
+
self.session_id: Optional[str] = None
|
| 10 |
+
|
| 11 |
+
def create_session(self) -> str:
|
| 12 |
+
"""Create a new browser session"""
|
| 13 |
+
response = requests.post(f"{self.base_url}/session/create")
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
data = response.json()
|
| 16 |
+
self.session_id = data["session_id"]
|
| 17 |
+
return self.session_id
|
| 18 |
+
|
| 19 |
+
def close_session(self):
|
| 20 |
+
"""Close the current session"""
|
| 21 |
+
if not self.session_id:
|
| 22 |
+
raise ValueError("No active session")
|
| 23 |
+
|
| 24 |
+
response = requests.delete(f"{self.base_url}/session/{self.session_id}")
|
| 25 |
+
response.raise_for_status()
|
| 26 |
+
self.session_id = None
|
| 27 |
+
|
| 28 |
+
def navigate(self, url: str) -> dict:
|
| 29 |
+
"""Navigate to a URL"""
|
| 30 |
+
if not self.session_id:
|
| 31 |
+
raise ValueError("No active session")
|
| 32 |
+
|
| 33 |
+
response = requests.post(
|
| 34 |
+
f"{self.base_url}/session/{self.session_id}/navigate",
|
| 35 |
+
json={"url": url}
|
| 36 |
+
)
|
| 37 |
+
response.raise_for_status()
|
| 38 |
+
return response.json()
|
| 39 |
+
|
| 40 |
+
def click(self, selector: str, selector_type: str = "css") -> dict:
|
| 41 |
+
"""Click an element"""
|
| 42 |
+
if not self.session_id:
|
| 43 |
+
raise ValueError("No active session")
|
| 44 |
+
|
| 45 |
+
response = requests.post(
|
| 46 |
+
f"{self.base_url}/session/{self.session_id}/click",
|
| 47 |
+
json={"selector": selector, "selector_type": selector_type}
|
| 48 |
+
)
|
| 49 |
+
response.raise_for_status()
|
| 50 |
+
return response.json()
|
| 51 |
+
|
| 52 |
+
def type_text(self, selector: str, text: str, selector_type: str = "css", clear_first: bool = True) -> dict:
|
| 53 |
+
"""Type text into an element"""
|
| 54 |
+
if not self.session_id:
|
| 55 |
+
raise ValueError("No active session")
|
| 56 |
+
|
| 57 |
+
response = requests.post(
|
| 58 |
+
f"{self.base_url}/session/{self.session_id}/type",
|
| 59 |
+
json={
|
| 60 |
+
"selector": selector,
|
| 61 |
+
"text": text,
|
| 62 |
+
"selector_type": selector_type,
|
| 63 |
+
"clear_first": clear_first
|
| 64 |
+
}
|
| 65 |
+
)
|
| 66 |
+
response.raise_for_status()
|
| 67 |
+
return response.json()
|
| 68 |
+
|
| 69 |
+
def screenshot(self, save_path: Optional[str] = None) -> str:
|
| 70 |
+
"""Take a screenshot and optionally save it"""
|
| 71 |
+
if not self.session_id:
|
| 72 |
+
raise ValueError("No active session")
|
| 73 |
+
|
| 74 |
+
response = requests.get(f"{self.base_url}/session/{self.session_id}/screenshot")
|
| 75 |
+
response.raise_for_status()
|
| 76 |
+
data = response.json()
|
| 77 |
+
|
| 78 |
+
if data["success"]:
|
| 79 |
+
screenshot_data = data["screenshot"]
|
| 80 |
+
|
| 81 |
+
if save_path:
|
| 82 |
+
# Decode and save the image
|
| 83 |
+
image_data = base64.b64decode(screenshot_data)
|
| 84 |
+
with open(save_path, "wb") as f:
|
| 85 |
+
f.write(image_data)
|
| 86 |
+
|
| 87 |
+
return screenshot_data
|
| 88 |
+
else:
|
| 89 |
+
raise Exception(f"Screenshot failed: {data['message']}")
|
| 90 |
+
|
| 91 |
+
def get_page_info(self) -> dict:
|
| 92 |
+
"""Get information about the current page"""
|
| 93 |
+
if not self.session_id:
|
| 94 |
+
raise ValueError("No active session")
|
| 95 |
+
|
| 96 |
+
response = requests.get(f"{self.base_url}/session/{self.session_id}/page-info")
|
| 97 |
+
response.raise_for_status()
|
| 98 |
+
return response.json()
|
| 99 |
+
|
| 100 |
+
def execute_js(self, script: str) -> dict:
|
| 101 |
+
"""Execute JavaScript on the page"""
|
| 102 |
+
if not self.session_id:
|
| 103 |
+
raise ValueError("No active session")
|
| 104 |
+
|
| 105 |
+
response = requests.get(
|
| 106 |
+
f"{self.base_url}/session/{self.session_id}/execute-js",
|
| 107 |
+
params={"script": script}
|
| 108 |
+
)
|
| 109 |
+
response.raise_for_status()
|
| 110 |
+
return response.json()
|
| 111 |
+
|
| 112 |
+
# Example usage
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
# Replace with your Hugging Face Space URL
|
| 115 |
+
client = BrowserAutomationClient("https://your-username-browser-api.hf.space")
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
# Create session
|
| 119 |
+
session_id = client.create_session()
|
| 120 |
+
print(f"Created session: {session_id}")
|
| 121 |
+
|
| 122 |
+
# Navigate to a website
|
| 123 |
+
result = client.navigate("https://httpbin.org/forms/post")
|
| 124 |
+
print(f"Navigation result: {result}")
|
| 125 |
+
|
| 126 |
+
# Get page info
|
| 127 |
+
page_info = client.get_page_info()
|
| 128 |
+
print(f"Page title: {page_info['title']}")
|
| 129 |
+
print(f"Found {len(page_info['elements'])} interactive elements")
|
| 130 |
+
|
| 131 |
+
# Fill out a form
|
| 132 |
+
client.type_text("input[name='custname']", "John Doe")
|
| 133 |
+
client.type_text("input[name='custtel']", "123-456-7890")
|
| 134 |
+
client.type_text("textarea[name='comments']", "This is a test comment")
|
| 135 |
+
|
| 136 |
+
# Take a screenshot
|
| 137 |
+
screenshot_b64 = client.screenshot("screenshot.png")
|
| 138 |
+
print("Screenshot saved as screenshot.png")
|
| 139 |
+
|
| 140 |
+
# Submit the form
|
| 141 |
+
client.click("input[type='submit']")
|
| 142 |
+
|
| 143 |
+
# Take another screenshot after submission
|
| 144 |
+
client.screenshot("after_submit.png")
|
| 145 |
+
print("After submit screenshot saved")
|
| 146 |
+
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print(f"Error: {e}")
|
| 149 |
+
|
| 150 |
+
finally:
|
| 151 |
+
# Always close the session
|
| 152 |
+
if client.session_id:
|
| 153 |
+
client.close_session()
|
| 154 |
+
print("Session closed")
|