|
9 | 9 | import random |
10 | 10 | import shutil |
11 | 11 | import time |
| 12 | +from typing import List |
12 | 13 |
|
13 | | -from PIL import Image |
14 | 14 | from botcity.base import BaseBot, State |
15 | 15 | from botcity.base.utils import only_if_element |
16 | 16 | from bs4 import BeautifulSoup |
| 17 | +from PIL import Image |
17 | 18 | from selenium.common.exceptions import InvalidSessionIdException |
18 | 19 | from selenium.webdriver.common.action_chains import ActionChains |
| 20 | +from selenium.webdriver.common.by import By |
19 | 21 | from selenium.webdriver.common.keys import Keys |
| 22 | +from selenium.webdriver.remote.webelement import WebElement |
20 | 23 | from selenium.webdriver.support.ui import WebDriverWait |
21 | 24 |
|
22 | 25 | from . import config, cv2find |
23 | | -from .browsers import Browser, BROWSER_CONFIGS |
24 | | - |
| 26 | +from .browsers import BROWSER_CONFIGS, Browser |
25 | 27 |
|
26 | 28 | try: |
27 | 29 | from botcity.maestro import BotMaestroSDK |
@@ -867,6 +869,76 @@ def wait_for_downloads(self, timeout: int = 120000): |
867 | 869 | # waits for all the files to be completed |
868 | 870 | WebDriverWait(self._driver, timeout/1000, 1).until(wait_method) |
869 | 871 |
|
| 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 | + |
870 | 942 | ####### |
871 | 943 | # Mouse |
872 | 944 | ####### |
|
0 commit comments