1
votes

I want to access google maps with python, but at first you have to click accept cookies button and I don't know why but I keep getting this error:

File "file", line 12, in WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]'))).click() File "C:\Users\gassp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('PATH')
url = 'https://www.google.com/maps/'

driver.get(url)

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()
3

3 Answers

0
votes

According to https://selenium-python.readthedocs.io/api.html :

exception selenium.common.exceptions.TimeoutException(msg=None, screen=None, stacktrace=None) Bases: selenium.common.exceptions.WebDriverException Thrown when a command does not complete in enough time.

Hence you either need to increase the time in WebDriverWait(driver, 10) or check if the condition EC.element_to_be_clickable is even fulfillable.


Furthermore, try the following implementation:

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]')))
element.click()

That fixed a few problems at my end with a similar code structure.

0
votes

When I tried to access the site I did not found the xpath locator //*[@id="introAgreeButton"] in chrome dev tool. Not sure what element it refers to.

However when I commented that line and added the click event on the search button, I was able to get the results.

Also there is no submit() method available on driver instance.

Below was my trial using the code you have provided -

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@id='searchbox-searchbutton']"))).click()

Output :-

enter image description here

0
votes

The problem was that it didn't change between the main frame and the accept cookie frame. Here is the soulution code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('C:\\Users\\gassp\\OneDrive\\Namizje\\Python.projects\\chromedriver.exe')
url = 'https://www.google.com/maps/'

driver.get(url)

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]/span/span'))) 
agree.click()

#back to the main page
driver.switch_to_default_content()


WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()