我想從谷歌地圖上獲取這家商店的位置,但 selenium 無法找到它。這是為什么?
存盤谷歌地圖鏈接: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 = 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
注意:xPath 正確,頁面已完全加載。你認為問題是什么?
謝謝你
我嘗試了不同的 xPath 并讓頁面完全加載,也嘗試滾動了一下,但仍然。我想獲取位置的文本。
這是我的代碼:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=chrome_options)
driver.get(url)
time.sleep(5)
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
uj5u.com熱心網友回復:
第一個朋友,這樣找xpath是不對的。復制您的代碼并更正它是這樣的(定位元素 - 檔案
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
此外,必須等待元素可見才能提取其文本(顯式等待 - 檔案
這是建議更改的代碼:
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)
uj5u.com熱心網友回復:
我們看不到您的所有腳本,因此我將我自己的腳本與您的鏈接和 xpath 一起使用。我的代碼給出了正確的位置結果。您的問題可能出在您的代碼中,而不是在 xpath 運算式中。
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)
uj5u.com熱心網友回復:
使用相對路徑使其作業
location = driver.find_element('xpath','//button[@data-item-id="address"]/div[1]/div[2]/div[1]').text
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525886.html
下一篇:如何按類選擇元素并獲取文本?
