sel_server.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 rpyc
  12. from rpyc.utils.server import ThreadedServer # or ForkingServer
  13. from bs4 import BeautifulSoup
  14. import json
  15. def send(driver, cmd, params={}):
  16. resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
  17. url = driver.command_executor._url + resource
  18. body = json.dumps({'cmd': cmd, 'params': params})
  19. response = driver.command_executor._request('POST', url, body)
  20. # if response['status']:
  21. # raise Exception(response.get('value'))
  22. return response.get('value')
  23. def add_script(driver, script):
  24. send(driver, "Page.addScriptToEvaluateOnNewDocument", {"source": script})
  25. def init_webdriver():
  26. WebDriver.add_script = add_script
  27. dir_path = os.path.dirname(os.path.realpath(__file__))
  28. options = webdriver.ChromeOptions()
  29. # options.add_argument("--user-data-dir=/home/jared/.config/google-chrome/")
  30. # options.add_argument('--profile-directory="Profile 1"')
  31. options.add_argument('--disable-web-security')
  32. options.add_argument('--allow-running-insecure-content')
  33. options.add_argument('--disable-blink-features=AutomationControlled')
  34. driver = webdriver.Chrome( chrome_options=options)
  35. return driver
  36. #global driver
  37. class MyService(rpyc.Service):
  38. def process(self,url):
  39. self.driver.add_script('const setProperty = () => { Object.defineProperty(navigator, "webdriver", { get: () => false, }); }; setProperty();')
  40. print('add url.............')
  41. print(self.driver)
  42. def __init__(self):
  43. self.driver = None
  44. try:
  45. self.driver = init_webdriver()
  46. print(self.driver )
  47. except Exception as e:
  48. raise e
  49. finally:
  50. True
  51. pass
  52. def exposed_get_url(self,url):
  53. self.driver.add_script('const setProperty = () => { Object.defineProperty(navigator, "webdriver", { get: () => false, }); }; setProperty();')
  54. print('add url.............')
  55. print(self.driver)
  56. self.driver.get(url)
  57. # txt=self.driver.find_element_by_xpath("//div[@id='json']").text
  58. print(self.driver.page_source)
  59. soup = BeautifulSoup(self.driver.page_source,features="lxml")
  60. dict_from_json = json.loads(soup.find("body").text)
  61. return dict_from_json
  62. # return json.loads(self.driver.page_source)
  63. if __name__ == "__main__":
  64. server = ThreadedServer(MyService(), port = 12345,protocol_config={
  65. 'allow_public_attrs': True,
  66. })
  67. server.start()