browser_common.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from selenium import webdriver
  2. #from seleniumwire import webdriver
  3. import time
  4. #import networkx as nx
  5. #import dataset
  6. #import pickle
  7. #import codecs
  8. from selenium.webdriver.common.keys import Keys
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.support.ui import WebDriverWait, Select
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.common.keys import Keys
  13. from selenium.webdriver.remote.webdriver import WebDriver
  14. import sys
  15. import os
  16. import time
  17. import re
  18. def interceptor(request):
  19. del request.headers['Referer'] # Remember to delete the header first
  20. request.headers['Referer'] = 'https://www.google.com/' # Spoof the referer
  21. def send(driver, cmd, params={}):
  22. """
  23. Send command to chromium driver
  24. """
  25. resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
  26. url = driver.command_executor._url + resource
  27. body = json.dumps({'cmd': cmd, 'params': params})
  28. response = driver.command_executor._request('POST', url, body)
  29. if response['status']:
  30. raise Exception(response.get('value'))
  31. return response.get('value')
  32. def add_script(driver, script):
  33. """
  34. Inject script before loading page
  35. Cf: https://stackoverflow.com/a/47298910
  36. """
  37. send(driver, "Page.addScriptToEvaluateOnNewDocument", {"source": script})
  38. class JBrowser:
  39. def __init__(self):
  40. if os.name=='nt':
  41. os.chdir("C:\\Program Files\\Google\\Chrome\\Application\\")
  42. def set_profile_path(self,profilepath):
  43. self.profilepath=profilepath
  44. option = webdriver.ChromeOptions()
  45. option.add_argument('--disable-web-security')
  46. option.add_argument('--allow-running-insecure-content')
  47. # option.add_argument('--headless')
  48. # option.add_argument("window-size=1280,800")
  49. # option.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
  50. option.add_argument('--disable-blink-features=AutomationControlled')
  51. if profilepath is not None:
  52. if os.name=='nt' :
  53. option.add_argument("--user-data-dir=C:\\Users\\jared\\AppData\\Local\\Google\\Chrome\\User Data\\"+self.profilepath+"\\")
  54. # option.add_argument("--user-data-dir='C:\\Users\\jared\\AppData\\Local\\Google\\Chrome\\User Data\\"+self.profilepath+"\\'")
  55. # option.add_argument("--user-data-dir=C:\\Users\\jared\\AppData\\Local\\Google\\Chrome\\User Data\\")
  56. # option.add_argument("--profile-directory='"+self.profilepath+"'")
  57. else:
  58. option.add_argument("--user-data-dir="+self.profilepath)
  59. option.add_argument('--profile-directory="Profile 1"')
  60. option.add_experimental_option("excludeSwitches", ["enable-automation"])
  61. option.add_experimental_option('useAutomationExtension', False)
  62. self.option=option
  63. WebDriver.add_script = add_script
  64. driver = webdriver.Chrome(options=option)
  65. driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
  66. driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
  67. driver.request_interceptor = interceptor
  68. self.driver=driver
  69. driver.delete_all_cookies()
  70. # executor_url = driver.command_executor._url
  71. # session_id = driver.session_id
  72. def scrolling(self,pgnum):
  73. ub = self.driver.find_element_by_css_selector('body')
  74. for i in range(pgnum):
  75. ub.send_keys(Keys.PAGE_DOWN)
  76. if pgnum>1:
  77. time.sleep(0.5)
  78. def get(self,url):
  79. self.driver.get(url)
  80. def get_driver(self):
  81. return self.driver