Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

browser_switcher_service.cc(238)] XXX Init() error with Python Selenium Script with Chrome for Web Scraping

I was using a small python script along with chrome drivers to download manga from Mangafox. It used to run fine until a few days ago when I updated the Chrome Browser. The following error is shown every time I try it now:

[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

I have used the selenium module with chrome drivers. I have tried updating my web drivers, tried making the code loop or sleep until the web-page loads completely.

My Code is as follows:

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 = []

After this part is just a loop I used to download the images. But for some reason, an error is shown and the list made for Tags remains empty. The driver.find_elements_by_tag_name('a') fails completely.

like image 873
Wandering Sovereign Avatar asked Apr 20 '20 15:04

Wandering Sovereign


1 Answers

These error messages...

[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

...implies that the on_init_ method failed in std::make_unique<base::ScopedClosureRunner>(std::move(on_init)).


Analysis

These errors are defined in bluetooth_adapter_winrt.cc as follows:

  • Getting Radio failed. Chrome will be unable to change the power state by itself:

    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.";
    
  • Number of Powered Radios: 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();
    }
    

Deep Dive

These errors are the direct impact of the changes incorporated with google-chrome as per the details within the discussion in Chrome no longer accepts certificates that fallback to common name


Solution

Ensure that:

  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v84.0 level.
  • Chrome is updated to current Chrome Version 84.0 level. (as per ChromeDriver v84.0 release notes)
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.

Additional considerations

However it was observed that this error can be supressed by running Chrome as root user (administrator) on Linux, but that would be a deviation from the documentation in ChromeDriver - WebDriver for Chrome where it is mentioned:

A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing '--no-sandbox' flag when creating your WebDriver session, i.e. the ChromeDriver session as such a configuration is unsupported and highly discouraged.

Ideally, you need to configure your environment to run Chrome as a regular user instead.


Suppressing the error

Finally, as per the documentation in Selenium Chrome Driver: Resolve Error Messages Regarding Registry Keys and Experimental Options these error logs can be supressed by adding the argument:

excludeSwitches: ['enable-logging']

So your effective code block will be:

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/")
like image 108
undetected Selenium Avatar answered Oct 30 '22 09:10

undetected Selenium