gaspa93 / googlemaps-scraper

Google Maps reviews scraping
GNU General Public License v3.0
358 stars 131 forks source link

Autofocus processing was blocked because a document already has a focused element. #46

Open BatoolHamawi opened 11 months ago

BatoolHamawi commented 11 months ago

I'm unable to scarp the reviews, it showed me this DevTools listening on ws://127.0.0.1:57417/devtools/browser/4c960584-6cdf-450f-831f-a1175e7d6d6a [0723/113647.067:INFO:CONSOLE(0)] "Autofocus processing was blocked because a document already has a focused element.", source: https://www.google.com/maps/place/Al+Salaam+Mall/@21.5078941,39.2233532,15z/data=!4m8!3m7!1s0x15c3ce6cdb182a97:0x29f6012ad865f128!8m2!3d21.5078941!4d39.2233532!9m1!1b1!16s%2Fg%2F11bvt4d9_v?entry=ttu (0) ←[36m[Review 0]←[0m

any advice how to solve it, it was work last week

DakshinD commented 11 months ago

I ended up facing the same problem as you. Even when running in debug mode, I would have to manually click the new window for it to start working.

I added the following lines of code to the sort_by function in googlemaps.py, and while I now get reviews back, the Autofocus error is still outputted. This may be a temporary fix but you can give it a try for now til someone else can fix it properly.

       # move to sort button 
        sort_element = self.driver.find_element(By.XPATH, '//button[@data-value=\'Sort\']')
        actions = ActionChains(self.driver)
        actions.move_to_element(sort_element).perform()

The new sort_by function looks like this

    def sort_by(self, url, ind):

        self.driver.get(url)
        #self.__click_on_cookie_agreement()

        # move to sort button 
        sort_element = self.driver.find_element(By.XPATH, '//button[@data-value=\'Sort\']')
        actions = ActionChains(self.driver)
        actions.move_to_element(sort_element).perform()

        wait = WebDriverWait(self.driver, MAX_WAIT)

        # open dropdown menu
        clicked = False
        tries = 0
        while not clicked and tries < MAX_RETRY:
            try:
                menu_bt = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@data-value=\'Sort\']')))
                menu_bt.click()

                clicked = True
                time.sleep(3)
            except Exception as e:
                tries += 1
                self.logger.warn('Failed to click sorting button')

            # failed to open the dropdown
            if tries == MAX_RETRY:
                return -1

        #  element of the list specified according to ind
        recent_rating_bt = self.driver.find_elements_by_xpath('//div[@role=\'menuitemradio\']')[ind]
        recent_rating_bt.click()

        # wait to load review (ajax call)
        time.sleep(5)

        return 0
UNAVS commented 7 months ago

Does this supposed to open a Chrome test software window? Because there's no Chrome windows appeared once I ran the script. I got the same output as well, the reviews is 0. Already applied the solution above as well but still 0