Skip to content

Commit

Permalink
Merge pull request #2544 from seleniumbase/mostly-uc-mode-updates
Browse files Browse the repository at this point in the history
Mostly UC Mode updates
  • Loading branch information
mdmintz authored Feb 28, 2024
2 parents 5d9fdc0 + c521e82 commit fb24362
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 47 deletions.
4 changes: 2 additions & 2 deletions examples/offline_examples/test_demo_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_demo_page(self):
try:
self.hover_and_click("#myDropdown", "#dropOption2", timeout=1)
except Exception:
# Someone moved the mouse while the test ran
self.js_click("#dropOption2")
# Someone probably moved the mouse while the test ran
self.hover_and_js_click("#myDropdown", "#dropOption2")
self.assert_text("Link Two Selected", "h3")

# Click a button and then verify the expected results
Expand Down
3 changes: 2 additions & 1 deletion examples/raw_form_turnstile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

def open_the_form_turnstile_page(sb):
sb.driver.uc_open_with_reconnect(
"https://seleniumbase.io/apps/form_turnstile", reconnect_time=2.5,
"https://seleniumbase.io/apps/form_turnstile", reconnect_time=2.7,
)


def click_turnstile_and_verify(sb):
sb.scroll_to_bottom()
sb.driver.uc_switch_to_frame("iframe")
sb.driver.uc_click("span.mark")
sb.highlight("img#captcha-success", timeout=3.33)
Expand Down
19 changes: 19 additions & 0 deletions examples/raw_nopecha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from seleniumbase import SB

with SB(uc=True, test=True) as sb:
sb.driver.uc_open_with_reconnect("https://nopecha.com/demo/turnstile", 5)
sb.driver.uc_switch_to_frame("#example-container5 iframe")
sb.driver.uc_click("span.mark")

if sb.is_element_visible("#example-container0 iframe"):
sb.switch_to_frame("#example-container0 iframe")
if not sb.is_element_visible("circle.success-circle"):
sb.driver.uc_click("span.mark")
sb.switch_to_frame("#example-container0 iframe")
sb.assert_element("circle.success-circle")
sb.switch_to_parent_frame()
sb.switch_to_frame("#example-container5 iframe")
sb.assert_element("svg#success-icon", timeout=3)
sb.switch_to_parent_frame()
sb.set_messenger_theme(location="top_center")
sb.post_message("Selenium wasn't detected!", duration=3)
13 changes: 13 additions & 0 deletions examples/raw_pixelscan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from seleniumbase import SB

with SB(uc=True, incognito=True, test=True) as sb:
sb.driver.uc_open_with_reconnect("https://pixelscan.net/", 11)
sb.remove_elements("jdiv") # Remove chat widgets
sb.assert_text("No automation framework detected", "pxlscn-bot-detection")
sb.assert_text("You are not masking your fingerprint")
sb.highlight("span.text-success", loops=10)
sb.sleep(1)
sb.highlight("pxlscn-fingerprint-masking div", loops=10, scroll=False)
sb.sleep(1)
sb.highlight("div.bot-detection-context", loops=10, scroll=False)
sb.sleep(3)
2 changes: 1 addition & 1 deletion examples/raw_turnstile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def open_the_turnstile_page(sb):
sb.driver.uc_open_with_reconnect(
"https://seleniumbase.io/apps/turnstile", reconnect_time=2.5,
"https://seleniumbase.io/apps/turnstile", reconnect_time=2.7,
)


Expand Down
7 changes: 4 additions & 3 deletions examples/raw_uc_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from seleniumbase import SB

with SB(uc=True, test=True) as sb:
sb.driver.uc_open_with_reconnect("https://top.gg/", 4)
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
if not sb.is_text_visible("Discord Bots", "h1"):
sb.get_new_driver(undetectable=True)
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
sb.activate_demo_mode() # Highlight + show assertions
sb.assert_text("Discord Bots", "h1", timeout=3)
sb.highlight("h1", loops=3)
sb.set_messenger_theme(location="top_center")
sb.post_message("Selenium wasn't detected!", duration=3)
4 changes: 2 additions & 2 deletions examples/test_demo_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_demo_site(self):
try:
self.hover_and_click("#myDropdown", "#dropOption2", timeout=1)
except Exception:
# Someone moved the mouse while the test ran
self.js_click("#dropOption2")
# Someone probably moved the mouse while the test ran
self.hover_and_js_click("#myDropdown", "#dropOption2")
self.assert_text("Link Two Selected", "h3")

# Click a button and then verify the expected results
Expand Down
10 changes: 3 additions & 7 deletions examples/verify_undetected.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ class UndetectedTest(BaseCase):
def test_browser_is_undetected(self):
if not self.undetectable:
self.get_new_driver(undetectable=True)
self.driver.uc_open_with_reconnect(
"https://top.gg/", reconnect_time=4
)
self.driver.uc_open_with_reconnect("https://top.gg/", 5)
if not self.is_text_visible("Discord Bots", "h1"):
self.get_new_driver(undetectable=True)
self.driver.uc_open_with_reconnect(
"https://top.gg/", reconnect_time=5
)
self.driver.uc_open_with_reconnect("https://top.gg/", 5)
self.assert_text("Discord Bots", "h1", timeout=3)
self.set_messenger_theme(theme="air", location="top_center")
self.set_messenger_theme(location="top_center")
self.post_message("Selenium wasn't detected!", duration=2.8)
self._print("\n Success! Website did not detect Selenium! ")
2 changes: 1 addition & 1 deletion help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ driver.uc_click(
selector, by="css selector",
timeout=settings.SMALL_TIMEOUT, reconnect_time=None)

driver.uc_switch_to_frame(frame)
driver.uc_switch_to_frame(frame, reconnect_time=None)
```

--------
Expand Down
30 changes: 11 additions & 19 deletions help_docs/uc_mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from seleniumbase import Driver

driver = Driver(uc=True)
driver.uc_open_with_reconnect("https://nowsecure.nl/#relax", 5)
driver.uc_open_with_reconnect("https://top.gg/", 6)
driver.quit()
```

Expand All @@ -35,30 +35,22 @@ driver.quit()
from seleniumbase import SB

with SB(uc=True) as sb:
sb.driver.uc_open_with_reconnect("https://nowsecure.nl/#relax", 5)
sb.driver.uc_open_with_reconnect("https://top.gg/", 6)
```

👤 Here's a longer example, which includes retries and a captcha-click failsafe for bypassing detection:
👤 Here's a longer example, which includes a retry if the CAPTCHA isn't bypassed on the first attempt:

```python
from seleniumbase import SB

with SB(uc=True, test=True) as sb:
sb.driver.uc_open_with_tab("https://nowsecure.nl/#relax")
sb.sleep(1.2)
if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
sb.get_new_driver(undetectable=True)
sb.driver.uc_open_with_reconnect(
"https://nowsecure.nl/#relax", reconnect_time=3
)
sb.sleep(1.2)
if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
if sb.is_element_visible('iframe[src*="challenge"]'):
with sb.frame_switch('iframe[src*="challenge"]'):
sb.click("span.mark")
sb.sleep(2)
sb.activate_demo_mode()
sb.assert_text("OH YEAH, you passed!", "h1", timeout=3)
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
if not sb.is_text_visible("Discord Bots", "h1"):
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
sb.assert_text("Discord Bots", "h1", timeout=3)
sb.highlight("h1", loops=3)
sb.set_messenger_theme(location="top_center")
sb.post_message("Selenium wasn't detected!", duration=3)
```

👤 Here's an example where clicking the checkbox is required, even for humans: (Commonly seen with forms that are CAPTCHA-protected.)
Expand Down Expand Up @@ -111,7 +103,7 @@ driver.uc_click(
selector, by="css selector",
timeout=settings.SMALL_TIMEOUT, reconnect_time=None)

driver.uc_switch_to_frame(frame)
driver.uc_switch_to_frame(frame, reconnect_time=None)
```

(Note that the `reconnect_time` is used to specify how long the driver should be disconnected from Chrome to prevent detection before reconnecting again.)
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.24.1"
__version__ = "4.24.2"
9 changes: 3 additions & 6 deletions seleniumbase/console_scripts/sb_mkdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,9 @@ def main():
' self.assert_text("Typing Text!", \'[name="preText2"]\')'
)
data.append(' self.assert_text("Automation Practice", "h3")')
data.append(" try:")
data.append(" self.hover_and_click(")
data.append(' "#myDropdown", "#dropOption2", timeout=1)')
data.append(" except Exception:")
data.append(" # Someone moved the mouse while the test ran")
data.append(' self.js_click("#dropOption2")')
data.append(
' self.hover_and_js_click("#myDropdown", "#dropOption2")'
)
data.append(' self.assert_text("Link Two Selected", "h3")')
data.append(' self.assert_text("This Text is Green", "#pText")')
data.append(' self.click("#myButton")')
Expand Down
24 changes: 21 additions & 3 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ def uc_special_open_if_cf(


def uc_open(driver, url):
if url.startswith("//"):
url = "https:" + url
elif ":" not in url:
url = "https://" + url
if (url.startswith("http:") or url.startswith("https:")):
time.sleep(0.05)
with driver:
Expand All @@ -407,6 +411,10 @@ def uc_open(driver, url):


def uc_open_with_tab(driver, url):
if url.startswith("//"):
url = "https:" + url
elif ":" not in url:
url = "https://" + url
if (url.startswith("http:") or url.startswith("https:")):
time.sleep(0.05)
with driver:
Expand All @@ -422,6 +430,10 @@ def uc_open_with_reconnect(driver, url, reconnect_time=None):
"""Open a url, disconnect chromedriver, wait, and reconnect."""
if not reconnect_time:
reconnect_time = constants.UC.RECONNECT_TIME
if url.startswith("//"):
url = "https:" + url
elif ":" not in url:
url = "https://" + url
if (url.startswith("http:") or url.startswith("https:")):
driver.execute_script('window.open("%s","_blank");' % url)
driver.close()
Expand Down Expand Up @@ -455,14 +467,20 @@ def uc_click(
driver.js_click(selector, by=by, timeout=timeout)


def uc_switch_to_frame(driver, frame):
def uc_switch_to_frame(driver, frame, reconnect_time=None):
from selenium.webdriver.remote.webelement import WebElement
if isinstance(frame, WebElement):
driver.reconnect(0.15)
if not reconnect_time:
driver.reconnect(0.15)
else:
driver.reconnect(reconnect_time)
driver.switch_to.frame(frame)
else:
iframe = driver.locator(frame)
driver.reconnect(0.15)
if not reconnect_time:
driver.reconnect(0.15)
else:
driver.reconnect(reconnect_time)
driver.switch_to.frame(iframe)


Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/undetected/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def uc_click(
else:
super().click()
if not reconnect_time:
self._parent.reconnect(0.1)
self._parent.reconnect(0.45)
else:
self._parent.reconnect(reconnect_time)

Expand Down

0 comments on commit fb24362

Please sign in to comment.