当前位置:   article > 正文

undetected_chromedriver 控制已打开的浏览器_undetected-chromedriver端口控制

undetected-chromedriver端口控制
  1. def get_options():
  2. options = ChromeOptions()
  3. options.add_argument("--disable-blink-features=AutomationControlled")
  4. # options.add_experimental_option("excludeSwitches", ["enable-automation"])
  5. # options.add_experimental_option("useAutomationExtension", False)
  6. options.add_argument("--disable-extensions")
  7. options.add_argument("--no-default-browser-check")
  8. options.add_argument("--no-first-run")
  9. options.add_argument("--disable-popup-blocking")
  10. options.add_argument("--disable-default-apps")
  11. options.add_argument("--disable-infobars")
  12. # options.add_argument("--disable-gpu")
  13. # options.add_argument("--disable-notifications")
  14. options.add_argument("--disable-translate")
  15. options.add_argument("--disable-device-discovery-notifications")
  16. options.add_argument("--no-sandbox")
  17. # options.add_argument("--disable-dev-shm-usage")
  18. options.add_argument("--window-size=1920x1080")
  19. options.add_argument("--disable-features=VizDisplayCompositor")
  20. return options
  21. def getchromeversion():
  22. if 'win' in sys.platform:
  23. import winreg
  24. key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
  25. chrome_version = winreg.QueryValueEx(key, 'version')[0]
  26. return chrome_version
  27. else:
  28. from webdriver_manager.utils import get_browser_version_from_os
  29. browserVersion = get_browser_version_from_os("google-chrome") # 获取当前系统chrome浏览器的版本号
  30. print(f'Chrome version is {browserVersion}')
  31. mainBrowserVersion = browserVersion.split(".")[0] # 获取chrome浏览器的主版本号
  32. return browserVersion
  33. def get_session_url(port):
  34. url = f'http://127.0.0.1:{port}/json/version'
  35. try:
  36. res = requests.get(url)
  37. print(res.text)
  38. webSocketDebuggerUrl = json.loads(res.text)['webSocketDebuggerUrl']
  39. except:
  40. # 命令行启动
  41. time.sleep(random.randint(4, 6))
  42. def start_brower():
  43. from undetected_chromedriver import Chrome, ChromeOptions, find_chrome_executable
  44. user_data_name = "chrome1"
  45. port = 9315
  46. user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
  47. current_path = os.path.dirname(os.path.realpath(sys.argv[0]))
  48. # 浏览器用户配置文件
  49. user_data_dir = os.path.join(current_path, user_data_name)
  50. # 浏览器版本号
  51. version = getchromeversion().split('.')[0]
  52. # 下载驱动
  53. currentpath = os.path.dirname(os.path.realpath(sys.argv[0]))
  54. executable_path = os.path.join(currentpath, 'chromedriver_win32', f'chromedriver_{version}.exe')
  55. if not os.path.exists(executable_path):
  56. # get_chromedriver_version(path, version)
  57. print('自动下载驱动')
  58. # 浏览器路径
  59. chrome_path = find_chrome_executable()
  60. print('executable_path:', executable_path)
  61. print('user_data_dir:', user_data_dir)
  62. print('chrome_path:', chrome_path)
  63. option = get_options()
  64. option.add_argument(fr'--user-data-dir="{user_data_dir}"')
  65. option.add_argument(f"--user-agent={user_agent}")
  66. option.debugger_address = f"127.0.0.1:{port}"
  67. driver = Chrome(options=option, executable_path=executable_path)
  68. print(driver.title)
  69. time.sleep(2)
  70. driver.get('https://blog.csdn.net/lwdfzr/article/details/135410359')
  71. time.sleep(random.randint(3, 5))
undetected_chromedriver 通过端口控制已打开的浏览器时,会打开一个多余的空白窗口,

鼠标放在Chrome() 按ctrl+B查看源码,找到这段代码,注释掉

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/949290
推荐阅读
  

闽ICP备14008679号