1

我有一个小问题,但因为我绝对是初学者,我找不到答案......你能帮我吗?

所以,我经营一个小房地产经纪人业务,我想在 FB Marketplace 上抓取本地广告。

但是由于某种原因,当我需要使用 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)
4

0 回答 0