简体   繁体   English

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

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

From this link , I want to get all of the car names as a list but only thing i can get is this: ------------> Car name is: []这个链接,我想把所有的车名作为一个列表,但我唯一能得到的是:------------>车名是:[]

Here is the html code:这是 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>

Here is my python code:这是我的 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()

I get stuck Plese help我卡住了请帮忙

It looks like an error in the XPath expression you are using.您正在使用的 XPath 表达式中似乎有错误。 On my browser I am able to extract the list of car names using the following XPath: "//span[@class='h3 u-text-break-word']".在我的浏览器上,我可以使用以下 XPath 提取汽车名称列表:“//span[@class='h3 u-text-break-word']”。

EDIT编辑

The XPath will give you a list of elements. XPath 将为您提供元素列表。 For each element you can access the "text" attribute as in the following ( source ):对于每个元素,您可以访问“text”属性,如下所示( source ):

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

Let me know if it is good for you.让我知道它是否对你有好处。

PS: I had not seen the answer below. PS:我没有看到下面的答案。 Exactly what you should do.正是你应该做的。

Try this below lines of code from image试试下面图片中的代码行在此处输入图像描述

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

相关问题 Python Selenium 获取页面源中的所有元素。 driver.find_elements(By.XPATH, "//*") - Python Selenium get all elements in page source. driver.find_elements(By.XPATH, "//*") “ find_element_by_xpath”和“ driver.find_elements(By.XPATH)”的区别是什么? - What is the difference in “find_element_by_xpath” and “driver.find_elements(By.XPATH)” Python Selenium:使用driver.find_elements获得不同的结果 - Python Selenium: Geting different results using driver.find_elements 迭代selenium webdriver的driver.find_elements - Iterating over selenium webdriver's driver.find_elements 蟒蛇硒-<class 'AttributeError'> 发生于 - len(driver.find_Elements(locatortype,locator)) &gt; 0 - Python Selenium-<class 'AttributeError'> occured for - len(driver.find_Elements(locatortype,locator)) > 0 通过href无法获取来自driver.find_elements的所有链接 - Get ALL links from driver.find_elements by href not working 无法使用 by.XPATH 找到元素 - Can't find element using by.XPATH driver.find_elements() 返回空列表 - driver.find_elements() is returning empty list find_element(By.XPATH) 不工作 python selenium - find_element(By.XPATH) not working python selenium Selenium find(By.XPATH) 无法使用 xpaths 数组定位元素 - Selenium find(By.XPATH) cannot locate element using array of xpaths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM