Skip to content

Commit f4e3496

Browse files
committed
Refactoring
1 parent db556c1 commit f4e3496

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

seleniumbase/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import collections
2+
import os
23
import pdb
34
try:
45
import pdbp # (Pdb+) --- Python Debugger Plus
@@ -34,11 +35,13 @@
3435
pdb.DefaultConfig.truncate_long_lines = False
3536
pdb.DefaultConfig.sticky_by_default = True
3637
colored_traceback.add_hook()
38+
os.environ["SE_AVOID_STATS"] = "true" # Disable Selenium Manager stats
3739
if sys.version_info >= (3, 7):
3840
webdriver.TouchActions = None # Lifeline for past selenium-wire versions
3941
if sys.version_info >= (3, 10):
4042
collections.Callable = collections.abc.Callable # Lifeline for nosetests
4143
del collections # Undo "import collections" / Simplify "dir(seleniumbase)"
44+
del os # Undo "import os" / Simplify "dir(seleniumbase)"
4245
del sys # Undo "import sys" / Simplify "dir(seleniumbase)"
4346
del webdriver # Undo "import webdriver" / Simplify "dir(seleniumbase)"
4447

seleniumbase/core/browser_launcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ def _set_chrome_options(
10071007
prefs["download.prompt_for_download"] = False
10081008
prefs["credentials_enable_service"] = False
10091009
prefs["local_discovery.notifications_enabled"] = False
1010-
prefs["safebrowsing.enabled"] = False
1010+
prefs["safebrowsing.enabled"] = False # Prevent PW "data breach" pop-ups
10111011
prefs["safebrowsing.disable_download_protection"] = True
10121012
prefs["omnibox-max-zero-suggest-matches"] = 0
10131013
prefs["omnibox-use-existing-autocomplete-client"] = 0
@@ -1534,7 +1534,7 @@ def _set_firefox_options(
15341534
f_pref_value = False
15351535
elif f_pref_value.isdigit():
15361536
f_pref_value = int(f_pref_value)
1537-
elif f_pref_value.isdecimal():
1537+
elif f_pref_value.replace(".", "", 1).isdigit():
15381538
f_pref_value = float(f_pref_value)
15391539
else:
15401540
pass # keep as string
@@ -2598,7 +2598,7 @@ def get_local_driver(
25982598
"credentials_enable_service": False,
25992599
"local_discovery.notifications_enabled": False,
26002600
"safebrowsing.disable_download_protection": True,
2601-
"safebrowsing.enabled": False,
2601+
"safebrowsing.enabled": False, # Prevent PW "data breach" pop-ups
26022602
"omnibox-max-zero-suggest-matches": 0,
26032603
"omnibox-use-existing-autocomplete-client": 0,
26042604
"omnibox-trending-zero-prefix-suggestions-on-ntp": 0,

seleniumbase/fixtures/base_case.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,9 +1251,17 @@ def focus(self, selector, by="css selector", timeout=None):
12511251
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
12521252
timeout = self.__get_new_timeout(timeout)
12531253
selector, by = self.__recalculate_selector(selector, by)
1254-
element = self.wait_for_element_visible(
1254+
element = self.wait_for_element_present(
12551255
selector, by=by, timeout=timeout
12561256
)
1257+
if not element.is_displayed():
1258+
css_selector = self.convert_to_css_selector(selector, by=by)
1259+
css_selector = re.escape(css_selector) # Add "\\" to special chars
1260+
css_selector = self.__escape_quotes_if_needed(css_selector)
1261+
script = """document.querySelector('%s').focus();""" % css_selector
1262+
self.execute_script(script)
1263+
self.__demo_mode_pause_if_active()
1264+
return
12571265
self.scroll_to(selector, by=by, timeout=timeout)
12581266
try:
12591267
element.send_keys(Keys.NULL)
@@ -1828,7 +1836,7 @@ def click_partial_link_text(self, partial_link_text, timeout=None):
18281836
elif self.slow_mode:
18291837
self.__slow_mode_pause_if_active()
18301838

1831-
def get_text(self, selector, by="css selector", timeout=None):
1839+
def get_text(self, selector="html", by="css selector", timeout=None):
18321840
self.__check_scope()
18331841
if not timeout:
18341842
timeout = settings.LARGE_TIMEOUT
@@ -2083,7 +2091,9 @@ def get_property(
20832091
return ""
20842092
return property_value
20852093

2086-
def get_text_content(self, selector, by="css selector", timeout=None):
2094+
def get_text_content(
2095+
self, selector="html", by="css selector", timeout=None
2096+
):
20872097
"""Returns the text that appears in the HTML for an element.
20882098
This is different from "self.get_text(selector, by="css selector")"
20892099
because that only returns the visible text on a page for an element,
@@ -3401,7 +3411,7 @@ def maximize_window(self):
34013411
self.driver.maximize_window()
34023412
self.__demo_mode_pause_if_active()
34033413

3404-
def switch_to_frame(self, frame, timeout=None):
3414+
def switch_to_frame(self, frame="iframe", timeout=None):
34053415
"""Wait for an iframe to appear, and switch to it. This should be
34063416
usable as a drop-in replacement for driver.switch_to.frame().
34073417
The iframe identifier can be a selector, an index, an id, a name,
@@ -6812,9 +6822,11 @@ def get_pdf_text(
68126822
try:
68136823
import cryptography
68146824
if cryptography.__version__ != "39.0.2":
6825+
del cryptography # To get newer ver
68156826
shared_utils.pip_install(
68166827
"cryptography", version="39.0.2"
68176828
)
6829+
import cryptography
68186830
except Exception:
68196831
shared_utils.pip_install(
68206832
"cryptography", version="39.0.2"

seleniumbase/fixtures/page_actions.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ def wait_for_element_not_visible(
10171017
def wait_for_text_not_visible(
10181018
driver,
10191019
text,
1020-
selector,
1020+
selector="html",
10211021
by="css selector",
10221022
timeout=settings.LARGE_TIMEOUT,
10231023
):
@@ -1060,7 +1060,7 @@ def wait_for_text_not_visible(
10601060
def wait_for_exact_text_not_visible(
10611061
driver,
10621062
text,
1063-
selector,
1063+
selector="html",
10641064
by="css selector",
10651065
timeout=settings.LARGE_TIMEOUT,
10661066
):
@@ -1686,19 +1686,50 @@ def assert_text(
16861686
by="css selector",
16871687
timeout=settings.SMALL_TIMEOUT,
16881688
):
1689+
selector, by = page_utils.recalculate_selector(selector, by)
16891690
wait_for_text_visible(
16901691
driver, text.strip(), selector, by=by, timeout=timeout
16911692
)
16921693

16931694

16941695
def assert_exact_text(
1695-
driver, text, selector, by="css selector", timeout=settings.SMALL_TIMEOUT
1696+
driver,
1697+
text,
1698+
selector="html",
1699+
by="css selector",
1700+
timeout=settings.SMALL_TIMEOUT,
16961701
):
1702+
selector, by = page_utils.recalculate_selector(selector, by)
16971703
wait_for_exact_text_visible(
16981704
driver, text.strip(), selector, by=by, timeout=timeout
16991705
)
17001706

17011707

1708+
def assert_non_empty_text(
1709+
driver,
1710+
selector,
1711+
by="css selector",
1712+
timeout=settings.SMALL_TIMEOUT,
1713+
):
1714+
selector, by = page_utils.recalculate_selector(selector, by)
1715+
wait_for_non_empty_text_visible(
1716+
driver, selector, by=by, timeout=timeout
1717+
)
1718+
1719+
1720+
def assert_text_not_visible(
1721+
driver,
1722+
text,
1723+
selector="html",
1724+
by="css selector",
1725+
timeout=settings.SMALL_TIMEOUT,
1726+
):
1727+
selector, by = page_utils.recalculate_selector(selector, by)
1728+
wait_for_text_not_visible(
1729+
driver, text.strip(), selector, by=by, timeout=timeout
1730+
)
1731+
1732+
17021733
def wait_for_element(
17031734
driver,
17041735
selector,
@@ -1748,6 +1779,7 @@ def wait_for_text(
17481779
by="css selector",
17491780
timeout=settings.LARGE_TIMEOUT,
17501781
):
1782+
selector, by = page_utils.recalculate_selector(selector, by)
17511783
return wait_for_text_visible(
17521784
driver=driver,
17531785
text=text,
@@ -1764,6 +1796,7 @@ def wait_for_exact_text(
17641796
by="css selector",
17651797
timeout=settings.LARGE_TIMEOUT,
17661798
):
1799+
selector, by = page_utils.recalculate_selector(selector, by)
17671800
return wait_for_exact_text_visible(
17681801
driver=driver,
17691802
text=text,
@@ -1779,6 +1812,7 @@ def wait_for_non_empty_text(
17791812
by="css selector",
17801813
timeout=settings.LARGE_TIMEOUT,
17811814
):
1815+
selector, by = page_utils.recalculate_selector(selector, by)
17821816
return wait_for_non_empty_text_visible(
17831817
driver=driver,
17841818
selector=selector,

seleniumbase/plugins/driver_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def Driver(
125125
uc_cdp=None, # Shortcut / Duplicate of "uc_cdp_events".
126126
uc_sub=None, # Shortcut / Duplicate of "uc_subprocess".
127127
log_cdp=None, # Shortcut / Duplicate of "log_cdp_events".
128+
server=None, # Shortcut / Duplicate of "servername".
128129
wire=None, # Shortcut / Duplicate of "use_wire".
129130
pls=None, # Shortcut / Duplicate of "page_load_strategy".
130131
):
@@ -247,6 +248,8 @@ def Driver(
247248
headless2 = False
248249
if protocol is None:
249250
protocol = "http" # For the Selenium Grid only!
251+
if server is not None and servername is None:
252+
servername = server
250253
if servername is None:
251254
servername = "localhost" # For the Selenium Grid only!
252255
use_grid = False

seleniumbase/plugins/sb_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def SB(
103103
uc_cdp=None, # Shortcut / Duplicate of "uc_cdp_events".
104104
uc_sub=None, # Shortcut / Duplicate of "uc_subprocess".
105105
log_cdp=None, # Shortcut / Duplicate of "log_cdp_events".
106+
server=None, # Shortcut / Duplicate of "servername".
106107
wire=None, # Shortcut / Duplicate of "use_wire".
107108
pls=None, # Shortcut / Duplicate of "page_load_strategy".
108109
sjw=None, # Shortcut / Duplicate of "skip_js_waits".
@@ -282,6 +283,8 @@ def SB(
282283
headless2 = False
283284
if protocol is None:
284285
protocol = "http" # For the Selenium Grid only!
286+
if server is not None and servername is None:
287+
servername = server
285288
if servername is None:
286289
servername = "localhost" # For the Selenium Grid only!
287290
if port is None:

0 commit comments

Comments
 (0)