【问题标题】:browser_switcher_service.cc(238)] XXX Init() error with Python Selenium Script with Chrome for Web Scrapingbrowser_switcher_service.cc(238)] XXX Init() error with Python Selenium Script with Chrome for Web Scraping
【发布时间】:2020-08-03 03:28:55
【问题描述】:

我正在使用一个小的 python 脚本和 chrome 驱动程序从 Mangafox 下载漫画。它曾经运行良好,直到几天前我更新了 Chrome 浏览器。我现在每次尝试都会显示以下错误:

[14664:14280:0420/202509.245:ERROR:browser_switcher_service.cc(238)] XXX Init()
[14664:14280:0420/202509.678:ERROR:device_event_log_impl.cc(162)] [20:25:09.679] Bluetooth: bluetooth_adapter_winrt.cc:1186 Getting Radio failed. Chrome will be unable to change the power state by itself.
[14664:14280:0420/202509.695:ERROR:device_event_log_impl.cc(162)] [20:25:09.696] Bluetooth: bluetooth_adapter_winrt.cc:1264 OnPoweredRadioAdded(), Number of Powered Radios: 1
[14664:14280:0420/202509.696:ERROR:device_event_log_impl.cc(162)] [20:25:09.696] Bluetooth: bluetooth_adapter_winrt.cc:1283 OnPoweredRadiosEnumerated(), Number of Powered Radios: 1

我已将 selenium 模块与 chrome 驱动程序一起使用。我已经尝试更新我的网络驱动程序,尝试让代码循环或休眠,直到网页完全加载。

我的代码如下:

from selenium import webdriver
import os
import urllib.request


Main = 'https://ww3.mangafox.online/'
Name = str(input('Enter Name of Manga as on \'ww3.Mangafox.online\':  '))
Name = Name.replace(' ', '-')
Name = Name.replace('\'', '-')

driver = webdriver.Chrome(r'C:\Users\freak\Assignments\SDP\Drivers\chromedriver.exe')
driver.get(Main + Name)

Tags = driver.find_elements_by_tag_name('a')
List = [] 
for Tag in Tags:
    List.append(str(Tag.get_attribute('href')))

dir = os.path.join('C:\\','Users', 'freak', 'Assignments', 'SDP', 'Test', Name.replace('-', '_'))
print('Checking the existence of folder; %s' % dir)
if not os.path.exists(dir):
    print('Folder not found. Attempting to create folder: %s' % dir)
    os.mkdir(dir)
    print('Folder successfully created')

Index = []
for i, element in enumerate(List):
    if (str(Name) + '/chapter') in element:
        Index.append(i)

Chapters = []

这部分之后只是我用来下载图像的循环。但是由于某种原因,会显示一个错误,并且为标签创建的列表仍然是空的。 driver.find_elements_by_tag_name('a') 完全失败。

【问题讨论】:

    标签: python selenium google-chrome selenium-webdriver web-scraping


    【解决方案1】:

    这些错误信息...

    [14664:14280:0420/202509.245:ERROR:browser_switcher_service.cc(238)] XXX Init()
    [14664:14280:0420/202509.678:ERROR:device_event_log_impl.cc(162)] [20:25:09.679] Bluetooth: bluetooth_adapter_winrt.cc:1186 Getting Radio failed. Chrome will be unable to change the power state by itself.
    [14664:14280:0420/202509.695:ERROR:device_event_log_impl.cc(162)] [20:25:09.696] Bluetooth: bluetooth_adapter_winrt.cc:1264 OnPoweredRadioAdded(), Number of Powered Radios: 1
    [14664:14280:0420/202509.696:ERROR:device_event_log_impl.cc(162)] [20:25:09.696] Bluetooth: bluetooth_adapter_winrt.cc:1283 OnPoweredRadiosEnumerated(), Number of Powered Radios: 1
    

    ...暗示on_init_ 方法在std::make_unique<base::ScopedClosureRunner>(std::move(on_init)) 中失败。


    分析

    这些错误在bluetooth_adapter_winrt.cc 中定义如下:

    • 获取电台失败。 Chrome 将无法自行更改电源状态

      void BluetoothAdapterWinrt::OnGetRadio(base::ScopedClosureRunner on_init,
                             ComPtr<IRadio> radio) {
        DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
        if (radio) {
          radio_ = std::move(radio);
          radio_was_powered_ = GetState(radio_.Get()) == RadioState_On;
          radio_state_changed_token_ = AddTypedEventHandler(
          radio_.Get(), &IRadio::add_StateChanged,
          base::BindRepeating(&BluetoothAdapterWinrt::OnRadioStateChanged,
                      weak_ptr_factory_.GetWeakPtr()));
          if (!radio_state_changed_token_)
            BLUETOOTH_LOG(ERROR) << "Adding Radio State Changed Handler failed.";
          return;
        }
        // This happens within WoW64, due to an issue with non-native APIs.
        BLUETOOTH_LOG(ERROR)
            << "Getting Radio failed. Chrome will be unable to change the power "
           "state by itself.";
      
    • 有源收音机数量:1

      void BluetoothAdapterWinrt::OnPoweredRadioAdded(IDeviceWatcher* watcher,
                                                      IDeviceInformation* info) {
        if (++num_powered_radios_ == 1)
          NotifyAdapterPoweredChanged(true);
        BLUETOOTH_LOG(ERROR) << "OnPoweredRadioAdded(), Number of Powered Radios: "
                             << num_powered_radios_;
      }
      void BluetoothAdapterWinrt::OnPoweredRadioRemoved(
          IDeviceWatcher* watcher,
          IDeviceInformationUpdate* update) {
        if (--num_powered_radios_ == 0)
          NotifyAdapterPoweredChanged(false);
        BLUETOOTH_LOG(ERROR) << "OnPoweredRadioRemoved(), Number of Powered Radios: "
                             << num_powered_radios_;
      }
      void BluetoothAdapterWinrt::OnPoweredRadiosEnumerated(IDeviceWatcher* watcher,
                                                            IInspectable* object) {
        BLUETOOTH_LOG(ERROR)
            << "OnPoweredRadiosEnumerated(), Number of Powered Radios: "
            << num_powered_radios_;
        // Destroy the ScopedClosureRunner, triggering the contained Closure to be
        // run. Note this may destroy |this|.
        DCHECK(on_init_);
        on_init_.reset();
      }
      

    深入研究

    根据Chrome no longer accepts certificates that fallback to common name 中讨论的详细信息,这些错误是与 合并的更改的直接影响


    解决方案

    确保:

    • Selenium 升级到当前级别 Version 3.141.59
    • ChromeDriver 已更新到当前的ChromeDriver v84.0 级别。
    • Chrome 已更新到当前的 Chrome 版本 84.0 级别。 (根据ChromeDriver v84.0 release notes
    • 如果您的基本 Web 客户端 版本太旧,请卸载它并安装最新的 GA 和发布版本的 Web 客户端

    其他注意事项

    但据观察,可以通过在 Linux 上以 root 用户 (administrator) 身份运行 Chrome 来抑制此错误,但这与ChromeDriver - WebDriver for Chrome 中提到的文档:

    Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户(管理员)身份运行 Chrome。虽然可以通过在创建 WebDriver 会话时传递 '--no-sandbox' 标志来解决此问题,即 ChromeDriver 会话,因为这种配置不受支持且非常不鼓励。

    理想情况下,您需要将环境配置为以普通用户身份运行 Chrome。


    抑制错误

    最后,根据Selenium Chrome Driver: Resolve Error Messages Regarding Registry Keys and Experimental Options 中的文档,可以通过添加参数来抑制这些错误日志:

    excludeSwitches: ['enable-logging']
    

    所以你的有效代码块将是:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_experimental_option("excludeSwitches", ["enable-logging"])
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.google.com/")
    

    【讨论】:

      【解决方案2】:

      您是否尝试过改用 Firefox 驱动程序?
      https://github.com/mozilla/geckodriver/releases

      你只需要更新你的代码:

      driver = webdriver.Firefox(executable_path='/path/to/driver')
      

      【讨论】:

      • 我做到了。它第一次工作,然后突然停止工作。
      • @WanderingSovereign 它以什么方式停止工作?它抛出了什么错误?
      • 这就是问题所在。它没有给我任何错误。它只是无法从网站上获取任何标签。
      • 当您说它无法从网站检索任何标签时,您的Tags 是空的,您的List 是空的,还是您的Index 是空的?另外,while True: 循环的目的是什么?之后的所有内容都应该在其中,还是只是Tags = driver.find_elements_by_tag_name('a')。如果是后者,那么程序将永远不会跳出该循环,因此您需要将其删除。
      • 对不起,我设置了 while true 循环,因为我想检查错误是否发生,因为网页尚未正确加载。它实际上不应该存在。我会改变它。是的,第一个列表“标签”返回为空。
      猜你喜欢
      • 2021-09-03
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多