[英]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.
正是你应该做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.