精神残废 2025-03-01 12:02 采纳率: 70.8%
浏览 11

python+selenium调试启动新版谷歌浏览器的问题

浏览器版本133
旧版 12和11
调试启动代码


import subprocess
cmd = 'chrome.exe --remote-debugging-port=8234 --user-data-dir="C:\\Users\\BigFish\\AppData\\Local\\Google\\Chrome\\User Data"'
subprocess.run(cmd)

selenium连接代码

import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service


class Spider():
    def __init__(self):
        self.service=Service(executable_path=driver_path)
        self.chrome_options = ChromeOptions()
        self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:8234")
        self.driver = webdriver.Chrome(options=self.chrome_options,service=self.service)
        self.driver.implicitly_wait(10)
        self.item_list=[]


    def run(self):
        self.driver.get(url)
        time.sleep(3)
        print(self.driver.page_source)


driver_path = r'C:\Users\BigFish\AppData\Local\Google\Chrome\Application\chromedriver.exe'

url='https://www.baidu.com'
item_list=[]
if __name__ == '__main__':
    spider=Spider()
    spider.run()

问题1:新版浏览器selenium只能链接唯一一个初始化标签页,其他标签页无法连接,初始化标签页关闭后就无法用selnium连接。旧版可以自动切换标签页

img

问题2:新版浏览器调试启动后只能通过selenium连接一次,再次执行代码就会返回下面的html ,浏览器也没有进行请求。旧版不会这样,会正常返回请求页面

<html><head>
    <meta charset="utf-8">
  </head>
  <body>
    <script src="background.js" type="text/javascript"></script>


</body></html>
  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-03-01 12:02
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解答

    你遇到的问题是使用 Python 和 Selenium 调试启动新版谷歌浏览器时,出现了问题。你的代码使用了 subprocess 模块来启动 Chrome 浏览器,但是仍然使用了旧版本的浏览器。

    解决方案是使用 selenium 库的 options 参数来指定浏览器版本。下面是一个示例代码:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument('--version=133')  # 指定浏览器版本
    options.add_argument('--remote-debugging-port=8234')
    options.add_argument('--user-data-dir=')
    
    driver = webdriver.Chrome(options=options)
    

    在上面的代码中,我们使用 Options 对象来指定浏览器版本为 133,并添加其他的启动参数。然后,我们使用 webdriver.Chrome 函数来启动浏览器。

    注意:在使用 selenium 库时,需要确保浏览器版本和 selenium 库版本相匹配。如果你的浏览器版本较高,可能需要使用较新的 selenium 库版本。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月1日