Skip to content

Commit 861203e

Browse files
authored
Merge pull request #11 from hhslepicka/find_elem_upload
ENH: Add find_element and find_elements to search the DOM for elements by id, xpath, css and much more.
2 parents a215140 + e18c5a8 commit 861203e

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

botcity/web/bot.py

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
import random
1010
import shutil
1111
import time
12+
from typing import List
1213

13-
from PIL import Image
1414
from botcity.base import BaseBot, State
1515
from botcity.base.utils import only_if_element
1616
from bs4 import BeautifulSoup
17+
from PIL import Image
1718
from selenium.common.exceptions import InvalidSessionIdException
1819
from selenium.webdriver.common.action_chains import ActionChains
20+
from selenium.webdriver.common.by import By
1921
from selenium.webdriver.common.keys import Keys
22+
from selenium.webdriver.remote.webelement import WebElement
2023
from selenium.webdriver.support.ui import WebDriverWait
2124

2225
from . import config, cv2find
23-
from .browsers import Browser, BROWSER_CONFIGS
24-
26+
from .browsers import BROWSER_CONFIGS, Browser
2527

2628
try:
2729
from botcity.maestro import BotMaestroSDK
@@ -867,6 +869,76 @@ def wait_for_downloads(self, timeout: int = 120000):
867869
# waits for all the files to be completed
868870
WebDriverWait(self._driver, timeout/1000, 1).until(wait_method)
869871

872+
def find_elements(self, selector: str, by: By = By.CSS_SELECTOR) -> List[WebElement]:
873+
"""Find elements using the specified selector with selector type specified by `by`.
874+
875+
Args:
876+
selector (str): The selector string to be used.
877+
by (str, optional): Selector type. Defaults to By.CSS_SELECTOR.
878+
[See more](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.common.by.By)
879+
880+
Returns:
881+
List[WebElement]: List of elements found.
882+
883+
**Example:**
884+
```python
885+
from botcity.web import By
886+
...
887+
# Find element by ID
888+
all_cells = self.find_elements("//td", By.XPATH)
889+
...
890+
```
891+
"""
892+
return self._driver.find_elements(by, selector)
893+
894+
def find_element(self, selector: str, by: str = By.CSS_SELECTOR) -> WebElement:
895+
"""Find an element using the specified selector with selector type specified by `by`.
896+
If more than one element is found, the first instance is returned.
897+
898+
Args:
899+
selector (str): The selector string to be used.
900+
by (str, optional): Selector type. Defaults to By.CSS_SELECTOR.
901+
[See more](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.common.by.By)
902+
903+
Returns:
904+
WebElement: The element found.
905+
906+
**Example:**
907+
```python
908+
from botcity.web import By
909+
...
910+
# Find element by ID
911+
elem = self.find_element("my_elem", By.ID)
912+
# Find element by XPath
913+
elem = self.find_element("//input[@type='submit']", By.XPATH)
914+
...
915+
```
916+
"""
917+
out = self.find_elements(selector=selector, by=by)
918+
if out:
919+
return out[0]
920+
921+
def set_file_input_element(self, element: WebElement, filepath: str):
922+
"""Configure the filepath for upload in a file element.
923+
Note: This method does not submit the form.
924+
925+
Args:
926+
element (WebElement): The file upload element.
927+
filepath (str): The path to the file to be uploaded.
928+
929+
**Example:**
930+
```python
931+
...
932+
# Find element
933+
elem = self.find_element("body > form > input[type=file]")
934+
# Set the filepath
935+
self.set_file_input_element(elem, "./test.txt")
936+
...
937+
```
938+
"""
939+
fpath = os.path.abspath(os.path.expanduser(os.path.expandvars(filepath)))
940+
element.send_keys(fpath)
941+
870942
#######
871943
# Mouse
872944
#######

0 commit comments

Comments
 (0)