So I'm trying to get my code to click the button in red in the image above, but no matter what I try selenium returns a NoSuchElementException. How would you go at doing this? Cause I can't figure it out.
Here's my code if you're interested:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Set up driver and remove weird error message that doesn't matter
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
# Get the webpage to interact with
driver.get('https://maps.google.com')
# Find search box and type into it
to_des = driver.find_element(By.ID, "searchboxinput").send_keys('McHenry Library')
enter = driver.find_element(By.ID, "searchboxinput").send_keys(Keys.ENTER)
# Line below should be clicking the button
directions = driver.find_element(By.XPATH, '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[4]/div[1]/button').click()
walking = driver.find_element(By.XPATH, '//*[@id="omnibox-directions"]/div/div[2]/div/div/div/div[4]/button').click()
from_des = driver.find_element(By.CLASS_NAME, 'tactile-searchbox-input')[2].send_keys('Oakes College')
CodePudding user response:
You can click on the Direction
text using xpath:
//div[text()='Directions']
If you want to click the Direction icon then, you can try with following Xpath:
//img[@alt='Directions']
If neither of these work, add step to check if the pane is open before clicking "Direction". It is possible that the pane is taking some time to open, and thus the element was not found.
CodePudding user response:
What you missing here is:
- To create good locators
- To add waits to access elements when they are ready to be accessed.
For this we normally useWebDriverWait
expected conditions explicit waits.
The following code works:
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://maps.google.com'
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, "searchboxinput"))).send_keys('McHenry Library', Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[jsaction*='directions']"))).click()
I'm using CSS Selector here to locate directions
button however this could be done with XPath as well.
So, in case you prefer using XPath this can be used instead of the last line where I used CSS Selector:
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@jsaction,'directions')]"))).click()