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

Question:

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:

<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:

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

Asked By: Fatih Sarikaya

||

Answers:

It looks like an error in the XPath expression you are using. On my browser I am able to extract the list of car names using the following XPath:

"//span[@class='h3 u-text-break-word']". 

EDIT

The XPath will give you a list of elements. For each element you can access the "text" attribute as in the following (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. Exactly what you should do.

Answered By: Herostere

Try this below lines of code from image
enter image description here

Answered By: Fayaz
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.