I have done a script that you search the location and a keyword, and extract name and address of all results from google maps, i want to save the phone number and the website too but i have no idea how to do it, i tried with XPATH, CSS Selector and class name, but it doesn't work because the order of infos changes if something is missing. I tried to do it checking if the icon is displayed but i don't know how to do it well, i am new in this kind of things
try:
icon = driver.find_element(By.XPATH, "/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[9]/div[6]/a/div[1]/div[1]/div/img")
website = driver.find_element(By.XPATH, "/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[7]/div[5]/a").get_attribute("href")
except NoSuchElementException:
website = "Not found"
try:
icon = driver.find_element(By.XPATH, "/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[7]/div[6]/button/div[1]/div[1]/div/img")
phone = driver.find_element(By.XPATH, "/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[7]/div[6]/button/div[1]/div[2]/div[1]").get_attribute("innerHTML")
except NoSuchElementException:
phone = "Not found"
CodePudding user response:
This should work:
try:
popup_with_infos = driver.find_element(By.CLASS_NAME, "Hu9e2e")
icon = popup_with_infos.find_element(By.XPATH, ".//img[contains(@src, 'public_gm_blue_24dp.png')]")
website = icon.find_element(By.XPATH, ".//ancestor::a").get_attribute("href")
except NoSuchElementException:
website = "Not found"
try:
popup_with_infos = driver.find_element(By.CLASS_NAME, "Hu9e2e")
icon = popup_with_infos.find_element(By.XPATH, ".//img[contains(@src, 'phone_gm_blue_24dp.png')]")
phone = icon.find_element(By.XPATH, "./../../..").get_attribute("innerText")
except NoSuchElementException:
phone = "Not found"