Issue
I am trying to get Selenium to click on "see more hours" and grab the hours shown on the next page on Google Maps. My issue is performing the first step: clicking.
What I have tried:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time
url = 'https://www.google.com/maps/place/211+Clover+Lane/@38.2567237,-85.6849465,13z/data=!4m10!1m2!2m1!1srestuarant!3m6!1s0x886974dd922d909f:0x9c4b766c51b27adc!8m2!3d38.2567237!4d-85.6499276!15sCgpyZXN0dWFyYW50WgwiCnJlc3R1YXJhbnSSARNldXJvcGVhbl9yZXN0YXVyYW504AEA!16s%2Fg%2F1tcvcn03?entry=ttu'
options = Options()
driver = webdriver.Chrome(executable_path=ChromeDriverManager(log_level=0).install(), options=options)
driver.get(url)
time.sleep(1)
response_overview = BeautifulSoup(driver.page_source, 'html.parser')
try:
wait = WebDriverWait(driver, 10)
# hours_bt = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label, 'See more hours')]")))
hours_bt = driver.find_element_by_xpath("//button[contains(@aria-label, 'Close')]")
print(hours_bt.text)
hours_bt.click()
time.sleep(1)
hours_page = BeautifulSoup(driver.page_source, 'html.parser')
hours = hours_page.find('div', class_='t39EBf GUrTXd')['aria-label'].replace('\u202f', ' ')
except Exception as e1:
print('e1', e1)
try:
hours_bt = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[11]/div[4]/button')))
hours_bt.click()
time.sleep(1)
hours_page = BeautifulSoup(driver.page_source, 'html.parser')
hours = hours_page.find('div', class_='t39EBf GUrTXd')['aria-label'].replace('\u202f', ' ')
except Exception as e2:
hours = None
print('e2', e2)
pass
The first try says that the element is not interactable and the second try times out. I'm at my wit's ends and appreciate some help. Versions: selenium==3.14.0,
Solution
To click on the element with text as See more hours you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label$='See more hours'][data-item-id='oh']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label, 'See more hours')][@data-item-id='oh']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.