Skip to content

Commit 1ec47d9

Browse files
committed
Add the choose_file() method for uploading a file
1 parent 0cb4193 commit 1ec47d9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,22 @@ def create_folder(self, folder):
17061706
except Exception:
17071707
pass
17081708

1709+
def choose_file(self, selector, file_path, by=By.CSS_SELECTOR,
1710+
timeout=settings.LARGE_TIMEOUT):
1711+
""" This method is used to choose a file to upload to a website.
1712+
It works by populating a file-chooser "input" field of type="file".
1713+
A relative file_path will get converted into an absolute file_path.
1714+
1715+
Example usage:
1716+
self.choose_file('input[type="file"], "my_dir/my_file.txt")
1717+
"""
1718+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
1719+
timeout = self.__get_new_timeout(timeout)
1720+
if page_utils.is_xpath_selector(selector):
1721+
by = By.XPATH
1722+
abs_path = os.path.abspath(file_path)
1723+
self.add_text(selector, abs_path, by=by, timeout=timeout)
1724+
17091725
def save_element_as_image_file(self, selector, file_name, folder=None):
17101726
""" Take a screenshot of an element and save it as an image file.
17111727
If no folder is specified, will save it to the current folder. """

0 commit comments

Comments
 (0)