sel_server.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from datetime import datetime
  2. import os
  3. import sys
  4. from selenium import webdriver
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.support.ui import WebDriverWait, Select
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.keys import Keys
  9. from selenium.webdriver.remote.webdriver import WebDriver
  10. import time
  11. import json
  12. import rpyc
  13. from rpyc.utils.server import ThreadedServer # or ForkingServer
  14. def send(driver, cmd, params={}):
  15. resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
  16. url = driver.command_executor._url + resource
  17. body = json.dumps({'cmd': cmd, 'params': params})
  18. response = driver.command_executor._request('POST', url, body)
  19. # if response['status']:
  20. # raise Exception(response.get('value'))
  21. return response.get('value')
  22. def add_script(driver, script):
  23. send(driver, "Page.addScriptToEvaluateOnNewDocument", {"source": script})
  24. def init_webdriver():
  25. WebDriver.add_script = add_script
  26. dir_path = os.path.dirname(os.path.realpath(__file__))
  27. options = webdriver.ChromeOptions()
  28. driver = webdriver.Chrome( chrome_options=options)
  29. return driver
  30. #global driver
  31. class MyService(rpyc.Service):
  32. def process(self,url):
  33. self.driver.add_script('const setProperty = () => { Object.defineProperty(navigator, "webdriver", { get: () => false, }); }; setProperty();')
  34. print('add url.............')
  35. print(self.driver)
  36. def __init__(self):
  37. self.driver = None
  38. try:
  39. self.driver = init_webdriver()
  40. print(self.driver )
  41. except Exception as e:
  42. raise e
  43. finally:
  44. True
  45. pass
  46. def exposed_get_url(self,url):
  47. self.driver.add_script('const setProperty = () => { Object.defineProperty(navigator, "webdriver", { get: () => false, }); }; setProperty();')
  48. print('add url.............')
  49. print(self.driver)
  50. self.driver.get(url)
  51. if __name__ == "__main__":
  52. server = ThreadedServer(MyService(), port = 12345,protocol_config={
  53. 'allow_public_attrs': True,
  54. })
  55. server.start()