Chrisyichuan commited on
Commit
3ba0e70
Β·
1 Parent(s): cb1701e

fix chrome

Browse files
Files changed (1) hide show
  1. mapcrunch_controller.py +63 -36
mapcrunch_controller.py CHANGED
@@ -11,53 +11,80 @@ from config import MAPCRUNCH_URL, SELECTORS, DATA_COLLECTION_CONFIG
11
 
12
  class MapCrunchController:
13
  def __init__(self, headless: bool = False):
14
- options = uc.ChromeOptions()
15
- options.add_argument(
16
- "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
17
- )
18
- options.add_argument("--window-size=1920,1080")
19
- options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
20
-
21
- if headless:
22
- options.add_argument("--headless=new")
 
 
 
 
 
23
 
24
- self.driver = uc.Chrome(options=options, use_subprocess=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  self.wait = WebDriverWait(self.driver, 10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- # Here we are injecting a script to the page to disable the browser detection.
28
- # Basically, we are setting the badBrowser property to 0, which is a property that is used to detect if the browser is being controlled by a script.
29
- # In the main.min.js, we can see some js code like this:
30
- # if (badBrowser) {
31
- # alert("Unsupported browser!");
32
- # } else {
33
- # window.panorama = { ... }
34
- # }
35
- self.driver.execute_cdp_cmd(
36
- "Page.addScriptToEvaluateOnNewDocument",
37
- {
38
- "source": """
39
- Object.defineProperty(window, 'badBrowser', {
40
- value: 0,
41
- writable: false,
42
- configurable: false
43
- });
44
- window.alert = function() {};
45
- Object.defineProperty(navigator, 'webdriver', {
46
- get: () => undefined
47
- });
48
- """
49
- },
50
- )
51
-
52
  for retry in range(3):
53
  try:
54
  self.driver.get(MAPCRUNCH_URL)
55
  time.sleep(3)
 
56
  break
57
  except Exception as e:
58
  if retry == 2:
59
  raise e
60
- print(f"Failed to load MapCrunch, retry {retry + 1}/3")
61
  time.sleep(2)
62
 
63
  def setup_clean_environment(self):
 
11
 
12
  class MapCrunchController:
13
  def __init__(self, headless: bool = False):
14
+ # Try to initialize ChromeDriver with version 137 (your current Chrome version)
15
+ try:
16
+ # Create fresh ChromeOptions for first attempt
17
+ options = uc.ChromeOptions()
18
+ options.add_argument("--no-sandbox")
19
+ options.add_argument("--disable-dev-shm-usage")
20
+ options.add_argument("--disable-gpu")
21
+ options.add_argument("--window-size=1920,1080")
22
+ options.add_argument("--disable-web-security")
23
+ options.add_argument("--disable-features=VizDisplayCompositor")
24
+ options.add_argument("--disable-blink-features=AutomationControlled")
25
+
26
+ if headless:
27
+ options.add_argument("--headless=new")
28
 
29
+ self.driver = uc.Chrome(options=options, use_subprocess=True, version_main=137)
30
+ print("βœ… ChromeDriver initialized successfully with version 137")
31
+ except Exception as e:
32
+ print(f"Failed with version 137: {e}")
33
+ try:
34
+ # Create fresh ChromeOptions for fallback attempt
35
+ options = uc.ChromeOptions()
36
+ options.add_argument("--no-sandbox")
37
+ options.add_argument("--disable-dev-shm-usage")
38
+ options.add_argument("--disable-gpu")
39
+ options.add_argument("--window-size=1920,1080")
40
+ options.add_argument("--disable-web-security")
41
+ options.add_argument("--disable-features=VizDisplayCompositor")
42
+ options.add_argument("--disable-blink-features=AutomationControlled")
43
+
44
+ if headless:
45
+ options.add_argument("--headless=new")
46
+
47
+ # Fallback to auto-detection
48
+ self.driver = uc.Chrome(options=options, use_subprocess=True)
49
+ print("βœ… ChromeDriver initialized successfully with auto-detection")
50
+ except Exception as e2:
51
+ print(f"Failed with auto-detection: {e2}")
52
+ raise Exception(f"Could not initialize ChromeDriver. Please update Chrome or check compatibility. Errors: {e}, {e2}")
53
+
54
  self.wait = WebDriverWait(self.driver, 10)
55
+
56
+ # Inject browser detection bypass script
57
+ try:
58
+ self.driver.execute_cdp_cmd(
59
+ "Page.addScriptToEvaluateOnNewDocument",
60
+ {
61
+ "source": """
62
+ Object.defineProperty(window, 'badBrowser', {
63
+ value: 0,
64
+ writable: false,
65
+ configurable: false
66
+ });
67
+ window.alert = function() {};
68
+ Object.defineProperty(navigator, 'webdriver', {
69
+ get: () => undefined
70
+ });
71
+ """
72
+ },
73
+ )
74
+ except Exception as e:
75
+ print(f"Warning: Could not inject browser detection script: {e}")
76
 
77
+ # Load MapCrunch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  for retry in range(3):
79
  try:
80
  self.driver.get(MAPCRUNCH_URL)
81
  time.sleep(3)
82
+ print("βœ… MapCrunch loaded successfully")
83
  break
84
  except Exception as e:
85
  if retry == 2:
86
  raise e
87
+ print(f"Failed to load MapCrunch, retry {retry + 1}/3: {e}")
88
  time.sleep(2)
89
 
90
  def setup_clean_environment(self):