3

when Google Map is to some level confirmed about a place search it redirects to the specific Google place url otherwise it returns a map search result page.

Google Map search for "manarama" is

https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6

which redirects to a Google Place URL

https://www.google.com/maps/place/Manarama,+29+Rd+No.+14A,+Dhaka+1209/@23.7505522,90.3616303,15z/data=!4m5!3m4!1s0x3755bf4dfc183459:0xb9127b8c3072c249!8m2!3d23.750523!4d90.3703851

Google Map search result page looks like the following link below when it is not confirmed about the specific place

https://www.google.com/maps/search/Mana/@24.211316,89.340686,8z/data=!3m1!4b1

import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        await page.goto("https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6", wait_until="networkidle")
        print(page.url) 
        await page.close()
        await browser.close()

asyncio.run(main())

Sometimes it returns the redirected URL, but most of the time, it doesn't. How to know the URL got redirected to a place URL for sure? the following StackOverflow post has similarities but couldn't make it work for my case

How to catch the redirect with a webapp using playwright

coolsaint
  • 1,291
  • 2
  • 16
  • 27
  • 1
    What problem did you face when adapting the linked answer to your case? – Charchit Agarwal Jun 26 '22 at 19:41
  • what would be the url_or_predicate in my case for page.expect_response(url_or_predicate, **kwargs) and if doesn't redirect then what will happen? because it will not always redirect as you can see. – coolsaint Jun 26 '22 at 19:45

1 Answers1

3

You can use expect_navigation.

In the comments you mentioned about what url to match for with the function. Almost all such playwright functions accept regex patterns. So when in doubt, just use regex. See the code below:

import asyncio
from playwright.async_api import async_playwright, TimeoutError
import re

pattern = re.compile(r"http.*://.+?/place.+")


async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        try:
            async with page.expect_navigation(url=pattern, timeout=7000) as resp:
                await page.goto(
                    "https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6",
                    wait_until='networkidle')
        except TimeoutError:
            print('place not found')
        else:
            print('navigated to place')

        print(page.url)
        await page.close()
        await browser.close()

asyncio.run(main())

In order to check whether the page navigated or not, just wrap the function inside a try..except block and pass a suitable timeout argument (in ms) to expect_navigation. Then if a Timeout error was raised, you know that there wasn't any url change which matched our pattern.

Charchit Agarwal
  • 2,829
  • 2
  • 8
  • 20
  • Thanks, @Charchit . It worked. Does it mean Google does the redirection in minimum 5 seconds? I tried 5000 at least. – coolsaint Jun 26 '22 at 20:17
  • 1
    it's more about your internet speed I guess, the only way to find such things are through trial and error. Just to make sure, `expect_navigation` expects the url to change to the one matching provided pattern, so the timeout should be large enough to ensure that happens. – Charchit Agarwal Jun 26 '22 at 20:21