少し問題がありますが、私はまったくの初心者なので、答えが見つかりません... 助けてもらえますか?
それで、私は小さな不動産業を営んでおり、FB マーケットプレイスのローカル広告をスクレイピングしたいと考えています。
しかし、何らかの理由で、xpath を使用する必要があるときに、これを取得する要素を見つけることができません。
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/span/text()"}
(Session info: chrome=97.0.4692.71)
だから私は次の質問があります:
- まず第一に、私は一体何を間違っているのですか?どうすればこの問題を解決できますか?
- 今まで行ったコードをどのように改善/デバッグできますか?
- Pythonでより効率的にスキルを向上させるにはどうすればよいですか?
ご協力ありがとうございました :)
# imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.common.keys import Keys
import time
# import csv
# Exceptions to handle unexpected faults!!!
# from selenium.common.exceptions import WebDriverException
# from selenium.common.exceptions import NoSuchElementException
# from selenium.common.exceptions import NoSuchFrameException
# from selenium.common.exceptions import NoSuchWindowException
# from selenium.common.exceptions import ElementNotInteractableException
# from selenium.common.exceptions import StaleElementReferenceException
# from selenium.common.exceptions import MoveTargetOutOfBoundsException
# from selenium.common.exceptions import UnexpectedAlertPresentException
# from selenium.common.exceptions import ElementClickInterceptedException
# from selenium.common.exceptions import JavascriptException
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome('F:/Real-Trader/chromedriver', chrome_options=chrome_options)
driver.get("http://www.facebook.com")
alert=WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Az összes cookie engedélyezése")]'))).click()
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='pass']")))
username.clear()
username.send_keys("@@@@@@@@@@@@@")
password.clear()
password.send_keys("*************")
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
time.sleep(5)
driver.get("https://www.facebook.com/marketplace/109375022413858/propertyforsale/")
time.sleep(5)
for j in range(0,0):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
anchors = driver.find_elements_by_tag_name('a')
anchors = [a.get_attribute('href') for a in anchors]
anchors = [a for a in anchors if str(a).startswith('https://www.facebook.com/marketplace/item/')]
links = []
for a in range(len(anchors)):
#print('------------------------')
the_link = str(anchors[a])
character_cut = int(anchors[a].find('/?'))
cut_link = the_link[:character_cut]
#print(cut_link)
links.append(cut_link)
ad = []
for elem in links:
print('------------------------')
print(elem)
driver.get(elem)
time.sleep(5)
# This is, what i should use succesfully....!
title = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/span/text()').text
location = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[7]/div[3]/div/div[1]/span/text()').text
price = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/div[1]/div/span/text()').text
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='Tovább']"))).click()
description = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[8]/div[2]/div/div/div/span/text()').text
seller_name = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[9]/div/div/div[2]/div/div/div/div/div[2]/div/div/div/div[1]/span/span/div/div/div/span').text
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='Eladó adatai']"))).click()
# seller_data = driver.find_element_by_xpath().text
# This is, what is actually working... :(
# description = driver.find_element_by_tag_name('div')
# print(description.text)
# ad.append(description)