Closed flairekq closed 2 months ago
URLs are Python strings, not variables. You need to put quotes around them. You probably had a loop with a try
/except
, which masked your actual issue and made the browser close. InvalidSessionIdException
would occur if you ran Selenium commands after the browser window was already closed.
Oh yes sorry I had a typo when I edited my code in the comment here.
My actual codes are:
from seleniumbase import SB
base_url = "https://www.thaiticketmajor.com/"
first_site_to_hit = "all-event/"
def is_pass_cloudflare(sb):
try:
sb.driver.assert_element("div#success")
return True
except:
print("[verify_cloudflare_success] failed")
return False
def main():
with SB(uc=True) as sb:
print("connecting to url")
sb.driver.uc_open_with_reconnect(base_url + first_site_to_hit, 4)
print("connected")
sb.driver.uc_click("button.btn-signin", 4)
with sb.frame_switch('iframe[src*="challenge"]'):
is_pass = is_pass_cloudflare(sb)
if not is_pass:
# try to manually click and test whether pass
sb.driver.uc_click("span.mark", 4)
print("manually clicked cloudflare")
sb.sleep(2)
with sb.frame_switch('iframe[src*="challenge"]'):
is_pass = is_pass_cloudflare(sb)
if not is_pass:
print("failed cloudflare")
return
print("passed cloudflare")
main()
Try with some of the non UC Mode examples. Running from Docker will likely expose that automation is being used, which will get you detected.
Hi @mdmintz, I managed to run my UC mode codes (same code above) on my local docker container on Windows. However, when I tried running the container on my m2 Mac, it hung at code related to UC like uc_click (apart from uc_open_with_reconnect). I can't change my code to non-UC mode as it fails Cloudflare when I use non-UC mode. Do you have any advice for this?
I referred to ([v4.25.4] Readme) and I have enabled rosetta on the Docker desktop.
I would run UC Mode outside of Docker, as Docker adds things that make bots detectable, and it's not easy to cover it up.
@flairekq I'm facing the same issues with my dockerized app where I'm using the SB context manager undetectable and headless. Running the container locally it hangs at uc_open_with_reconnect
. Even when I just run my code from my local machine (i.e. not from the container) I'm seeing the app crash with the selenium.common.exceptions.NoSuchWindowException: Message: Active window was already closed!
error.
I'm running my scraping code from a django-rq job (including when I run it locally), so maybe this is easy for sites to detect automation? Though I'm just assuming that it's the target site that is causing the app to crash and for me to lose my seleniumbase connection. I don't see anything different on the webpage.
Seems spotty - some runs get farther than others.
Hi, I've tried to test seleniumbase with docker locally. It builds successfully and runs however in some runs there would be
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
error forTraceback:
dockerfile (used the latest one committed with changes at Set up SeleniumBase, and entrypoint sections):
Thank you in advance.