繁体   English   中英

如何通过使用 driver.find_elements(By.XPATH,'') 和 Python Selenium 来获取汽车名称

[英]How can i get car names by using driver.find_elements(By.XPATH,'') with Python Selenium

这个链接,我想把所有的车名作为一个列表,但我唯一能得到的是:------------>车名是:[]

这是 html 代码:

<div class="headline-block u-margin-bottom-9"><span class="sponsored-badge">Sponsored</span><span class="h3 u-text-break-word">Opel Omega</span></div>

这是我的 python 代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


service = Service(executable_path='C:/Users/Wiveda/AppData/Local/Programs/Python/Python310/chromedriver.exe')

import requests

url = 'http://httpbin.org/cookies'

my_cookies = dict(cookies_are='Cookies parameter use to send cookies to the server')
r = requests.get(url, cookies=my_cookies)


driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))


options = Options()
options.headless = True  # hide GUI
options.add_argument("--window-size=1920,1080")  # set window size to native GUI size
options.add_argument("start-maximized")  # ensure window is full-screen

chrome_options = Options()
chrome_options.add_argument('--headless')

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--headless")
options.add_experimental_option('excludeSwitches', ['enable-logging'])


driver.maximize_window()
driver.get(
    "https://suchen.mobile.de/fahrzeuge/search.html?dam=0&isSearchRequest=true&ms=19000%3B20%3B%3B&ref=quickSearch&sb=rel&vc=Car")
print("Application title is :", driver.title)
print("Application url is ", driver.current_url)
carname = driver.find_elements(By.XPATH,"//span[@class='h3 u-text-break-word']")
print("Car name is :", carname)

driver.quit()

我卡住了请帮忙

您正在使用的 XPath 表达式中似乎有错误。 在我的浏览器上,我可以使用以下 XPath 提取汽车名称列表:“//span[@class='h3 u-text-break-word']”。

编辑

XPath 将为您提供元素列表。 对于每个元素,您可以访问“text”属性,如下所示( source ):

carname = driver.find_elements(By.XPATH,"//span[@class='h3 u-text-break-word']")
for name in carname:
    print(name.text)

让我知道它是否对你有好处。

PS:我没有看到下面的答案。 正是你应该做的。

试试下面图片中的代码行在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM