I've written a script in python to populate results after initiating a serach in the search box in google map. My script is doing this flawlessly. Now, I'm trying to click on each of the results to go one layer deep and parse the title from there.
When I run the script, I get one title successfully but then the script throws the same common error
element is not attached to the dom
, although I've taken all the measures to get rid of that.
I used here this keyword motels in new jersey
as search.
I've tried with:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.google.com/maps/search/")
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#searchboxinput"))).send_keys("motels in new jersey")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#searchbox-searchbutton"))).click()
while True:
try:
for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[class='section-result'] h3[class='section-result-title']"))):
# click on each of the results
item.click()
# the script now in inner page to parse the title
name = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"h1.section-hero-header-title-title"))).text
print(name)
# click on the "Back to results" link located on the top left to get back to the results page
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"button[class^='section-back-to-list-button']"))).click()
# wait for the spinner to be invisible
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "#searchbox[class*='loading']")))
# tried to get rid of staleness condition
wait.until(EC.staleness_of(item))
except Exception:
break
How can I click on different results to parse the title from inner pages?