Skip to content

Commit

Permalink
feat: smart wait for selenium exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
adeprez committed Sep 27, 2024
1 parent e4b693e commit 50ac0de
Show file tree
Hide file tree
Showing 8 changed files with 2,397 additions and 6,653 deletions.
165 changes: 165 additions & 0 deletions examples/example_export_and_run_trajectory.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from lavague import Trajectory\n",
"\n",
"data = {\n",
" \"run_id\": \"abc123\",\n",
" \"start_url\": \"https://us.puma.com/us/en\",\n",
" \"objective\": \"Go to PEFT\",\n",
" \"status\": \"success\",\n",
" \"output\": None,\n",
" \"viewport_size\": [1600, 800],\n",
" \"actions\": [\n",
" {\n",
" \"step_id\": \"abc123-1\",\n",
" \"url\": \"https://us.puma.com/us/en\",\n",
" \"action_type\": \"web_navigation\",\n",
" \"status\": \"completed\",\n",
" \"instruction\": \"Click on the search bar in the top right corner of the page\",\n",
" \"action_output\": [\n",
" {\n",
" \"xpath\": \"/html/body/div/div/div/nav/div/div[2]/div/button\",\n",
" \"navigation_command\": \"click\",\n",
" \"value\": \"\"\n",
" }\n",
" ]\n",
" }, {\n",
" \"step_id\": \"abc123-1\",\n",
" \"url\": \"https://us.puma.com/us/en\",\n",
" \"action_type\": \"web_navigation\",\n",
" \"status\": \"completed\",\n",
" \"instruction\": \"Type 'sneakers' into the search bar and press Enter\",\n",
" \"action_output\": [\n",
" {\n",
" \"xpath\": \"/html/body/div/div/div/nav/div/div[2]/div/div/div[2]/div/div/form/input\",\n",
" \"navigation_command\": \"set_value_and_enter\",\n",
" \"value\": \"sneakers\"\n",
" }\n",
" ]\n",
" }, {\n",
" \"step_id\": \"abc123-1\",\n",
" \"url\": \"https://us.puma.com/us/en/search?q=sneakers\",\n",
" \"action_type\": \"web_extraction\",\n",
" \"status\": \"completed\",\n",
" \"instruction\": \"Extract the following elements: The title or heading of the search results page\",\n",
" \"action_output\": [\n",
" {\n",
" \"xpath\": \"/html/body/div[1]/div[1]/main/div/section/nav[2]/div[1]/span/span\",\n",
" \"description\": \"Element containing the text indicating the number of search results found\",\n",
" \"text\": \"1563 RESULTS FOR 'SNEAKERS'\",\n",
" \"outer_html\": \"<span data-test-id='product-results'>1563 results for 'sneakers'</span>\"\n",
" }\n",
" ]\n",
" }\n",
"\n",
" ]\n",
"}\n",
"\n",
"trajectory = Trajectory.from_dict(data)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"from selenium.webdriver.chrome.options import Options\n",
"from selenium import webdriver\n",
"from selenium.webdriver.common.by import By\n",
"from selenium.webdriver.common.keys import Keys\n",
"from selenium.webdriver.common.action_chains import ActionChains\n",
"from selenium.webdriver.support.ui import WebDriverWait\n",
"from selenium.webdriver.support import expected_conditions as EC\n",
"chrome_options = Options()\n",
"\n",
"TIMEOUT = 10\n",
"\n",
"chrome_options.add_argument(\n",
" 'user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'\n",
" )\n",
"chrome_options.add_argument('--no-sandbox')\n",
"chrome_options.add_argument('--disable-web-security')\n",
"chrome_options.add_argument('--disable-site-isolation-trials')\n",
"viewport_size = (1600, 800)\n",
"\n",
"width = viewport_size[0]\n",
"height = viewport_size[1]\n",
"driver = webdriver.Chrome(options=chrome_options)\n",
"\n",
"driver.set_window_size(width, height)\n",
"viewport_height = driver.execute_script('return window.innerHeight;')\n",
"driver.set_window_size(width, 2 * height - viewport_height)\n",
"driver.get('https://us.puma.com/us/en')\n",
"\n",
"# Click on the search bar in the top right corner of the page\n",
"element = WebDriverWait(driver, TIMEOUT).until(EC.\n",
" element_to_be_clickable((By.XPATH, '/html/body/div/div/div/nav/div/div[2]/div/button')))\n",
"element.click()\n",
"\n",
"# Type 'sneakers' into the search bar and press Enter\n",
"element = WebDriverWait(driver, TIMEOUT).until(EC.\n",
" visibility_of_element_located((By.XPATH, '/html/body/div/div/div/nav/div/div[2]/div/div/div[2]/div/div/form/input')))\n",
"element.send_keys('sneakers' + Keys.ENTER)\n",
"\n",
"# Extract the following elements: The title or heading of the search results page\n",
"element = WebDriverWait(driver, TIMEOUT).until(EC.\n",
" visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/main/div/section/nav[2]/div[1]/span/span')))\n",
"element.text\n",
"\n",
"driver.quit()\n",
"\n"
]
}
],
"source": [
"from lavague.exporter.python_selenium import PythonSeleniumExporter\n",
"\n",
"exporter = PythonSeleniumExporter()\n",
"\n",
"code = exporter.export(trajectory)\n",
"\n",
"print(code)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"exec(code)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "lavague-Y10yuNgR-py3.12",
"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.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 50ac0de

Please sign in to comment.