i want to get the location this store from google maps but selenium is unable to locate it. why is that?
store google maps link: https://www.google.com/maps/place/Blaze Pizza/@24.5014283,54.387503,17z/data=!3m1!4b1!4m5!3m4!1s0x3e5e676982d20b17:0xe2c5b69e67e4c85d!8m2!3d24.5014283!4d54.3896917
by code segment:
location = driver.find_element('xpath','//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[9]/div[3]/button/div[1]/div[2]/div[1]').text
Note: xPath is correct, and page is fully loading. what do you think the problem is?
thank you
i tried different xPath and letting the page load fully, also tried scrolling a bit but still. i want to get the text of the location.
CodePudding user response:
First friend, it is not right to look for the xpath this way. Copying your code and correcting it would be like this (Locating Elements - Documentation
location = driver.find_element(By.XPATH,'//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[9]/div[3]/button/div[1]/div[2]/div[1]').text
In addition, it is necessary to wait for the element to be visible in order to extract its text (Explicit Waits - Documentation
Here's the code with proposed changes:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
(...)
driver.get("https://www.google.com/maps/place/Blaze Pizza/@24.5014283,54.387503,17z/data=!3m1!4b1!4m5!3m4!1s0x3e5e676982d20b17:0xe2c5b69e67e4c85d!8m2!3d24.5014283!4d54.3896917")
location = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((
By.XPATH, '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[9]/div[3]/button/div[1]/div[2]/div[1]')))
print(location.text)
CodePudding user response:
We can't see your all script so I use my own script with your link and xpath. My code gives the correct location result. Your problem may be in your code not in xpath expression.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium_stealth import stealth
options = webdriver.ChromeOptions()
options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36')
options.add_argument("--profile-directory=Default")
options.add_argument("--disable-extensions")
options.add_argument("--disable-plugins-discovery")
options.add_argument("--start-maximized")
options.add_argument("--incognito")
# options.add_argument("--headless")
executable_path = "Your Chromedriver Path"
driver = webdriver.Chrome(options=options, service=Service(executable_path))
driver.delete_all_cookies()
stealth(driver,
languages=["en-EN", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
url = "https://www.google.com/maps/place/Blaze Pizza/@24.5014283,54.387503,17z/data=!3m1!4b1!4m5!3m4!1s0x3e5e676982d20b17:0xe2c5b69e67e4c85d!8m2!3d24.5014283!4d54.3896917"
driver.get(url)
location = driver.find_element('xpath','//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[9]/div[3]/button/div[1]/div[2]/div[1]').text
print(location)