有一个问题写代码与 selenium 在python刮信息从depop上市为我的个人项目。具体的标题,价格,条件,大小和品牌。
我用过 selenium ,它一直给我的错误是没有这样的元素:无法找到元素:。所以在提取文本并将其保存到列表方面有点陷入死胡同。有什么建议吗?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
import requests
from bs4 import BeautifulSoup
# specify the URL you want to scrape
url = 'https://www.depop.com/products/_realvintage-vintage-south-beach-florida-embroidered/'
# results = requests.get(url)
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome("D:\\Selenium_python2\\chromedriver.exe", chrome_options=chrome_options)
driver.get(url)
time.sleep(2)
driver.find_element('xpath', '//*[@id="__next"]/div/div[3]/div[2]/button[2]').click()
time.sleep(1)
element = driver.find_element('xpath', '//*[@id="main"]/div[1]/div[3]/div/div[1]/p/text()')
print(element)
text = element.text
print(text)
1条答案
按热度按时间fhg3lkii1#
请检查它是否在任何
iframe
或shadow root
内。如果涉及到任何iframe,那么您需要切换到该iframe,然后使用
get_element
。driver.switchTo().frame(“<id of iframe>”)
这是一个了解Shadow DOM https://medium.com/@titus_on_testing/shadow-dom-in-python-selenium-df8d731b5005的好博客