0

This is my current code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import sys
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome('/Users/opulentbase/Downloads/chromedriver')

browser.get('https://www.gooogle.com/')

searchBar = browser.find_element_by_name('q')

userSearch = input("Please enter a search: ")

searchBar.send_keys(userSearch)

searchBar.send_keys(Keys.ENTER)

morePlaces = browser.find_element_by_class_name('i0vbXd')

morePlaces.click()

locations = browser.find_elements_by_class_name('dbg0pd')

locations.click()

When I get to the google maps portion of the search there are 20 results of businesses on the map. They are all the same class name (dbg0pd). I want to click each of these businesses for the business information popup to appear, perform a task, then go back to clicking the rest of the results and performing the task.

This is the screenshot of the results: https://prnt.sc/se0vy4

This is the popup that shows up once I click a business: https://prnt.sc/se0wi3

Thanks in advance!

opulentbase
  • 7
  • 1
  • 3

1 Answers1

0
locations = browser.find_elements_by_class_name('dbg0pd')
for location in locations:
    location.click()

    # insert your task here
JuliettVictor
  • 614
  • 5
  • 14
  • Thanks for the response. This code you just wrote out makes sense the only thing that doesn't is that there are 20 elements in this search result that have the same class which is dbg0pd. I want to be able to click each of these classes one after another in order. Once the first one gets clicked, I want it to perform the "task". Then I want it to click the next one and perform the same task. Until the task has been performed on all 20 elements. – opulentbase May 10 '20 at 05:59
  • try out the code above, it should do exactly that. Can you give an example of a task you want to perform? – JuliettVictor May 10 '20 at 06:32
  • This is a screenshot for what I currently have for my code: https://prnt.sc/se1ex6 the code right now clicks the first business performs the task, but then when it returns to the original page where the other 20 classes exist it doesn't click the 2nd class and perform the same task. – opulentbase May 10 '20 at 06:45
  • This does not work because the element references you collected in locations are no longer valid once you move to another page. A simple solution would be to first collect all the URLs from Google, and then crawl those websites in a second step. Alternatively, you could open the website in a new tab, which keeps the references in the first tab alive. – JuliettVictor May 10 '20 at 07:14
  • In the future, please post your code here instead of taking a screenshot – JuliettVictor May 10 '20 at 07:22
  • Sorry about that I am new to this community. I did not know that I have to open it in a new tab. How would I go about opening the window in a new tab with the current code I have right now? website = browser.find_element_by_link_text('Website') website.click() – opulentbase May 10 '20 at 07:39
  • see e.g. here: https://stackoverflow.com/questions/57730636/how-to-open-a-link-embeded-in-a-webelement-with-in-the-main-tab-in-a-new-tab-of – JuliettVictor May 10 '20 at 07:46
  • Hey Juilett, I finally figured out how to open the web page up in the new tab then close the tab and come back to the original window when the task is done. But when the task is done and the new tab is closed it gives me this error when it comes back to the old tab: selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed – opulentbase May 10 '20 at 09:18
  • you need to switch back to the first tab, using browser.switch_to.window. The answer by frianH in the link above shows you how. – JuliettVictor May 10 '20 at 16:29