Python Selenium使用xPATH查找元素返回为空

huangapple go评论72阅读模式
英文:

Python Selenium find_elements by xPATH returns nothing

问题

你的Python代码看起来没错,但是可能是因为网页加载延迟或者XPath选择器不准确导致无法提取文本。你可以尝试添加等待,确保页面加载完成后再提取文本。这是一个示例:

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('https://bid.cars/pl/search/results?search-type=filters&type=Automobile&year-from=1900&year-to=2024&make=Jeep&model=Cherokee&auction-type=All')

# 使用等待确保页面加载完成
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")))

all_spans = driver.find_elements(by=By.XPATH, value="//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")
for span in all_spans:
    print(span.text)

这个代码会等待页面元素加载完成,然后再提取文本。如果还有问题,可能需要检查XPath选择器是否准确匹配到你想要的元素。

英文:

Hi I am new in python selenium. I want to extract some information from the following page https://bid.cars/pl/search/results?search-type=filters&type=Automobile&year-from=1900&year-to=2024&make=Jeep&model=Cherokee&auction-type=All
The page returns list of sold cars. Each car has unique identifier (Eg."Numer: 0-35954378"). I want to extract value for all the cars. As a tart point I am trying to extract value 0-35954378.
Click to display screeshot.
Via SelectorsHub I retrieved the element Xpath //body[1]/section[1]/div[1]/div[1]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/ul[1]/li[1]/span[1]

My python code looks like below but it returns nothing

from selenium.webdriver.common.by import By
from selenium import webdriver

driver=webdriver.Chrome()
driver.get('https://bid.cars/pl/search/results?search-type=filters&type=Automobile&year-from=1900&year-to=2024&make=Jeep&model=Cherokee&auction-type=All')
all_spans = driver.find_elements(by=By.XPATH,value="//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")
for span in all_spans:
    print(span.text)

What I am doing wrong ?

答案1

得分: 0

你只需要等页面完全加载并显示所需元素。你可以通过使用selenium的waits来实现。

尝试这个:

all_spans = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")))
for span in all_spans:
    print(span.text)

所需导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

控制台输出:

Numer: 0-35954378

进程以退出代码0完成

更新: 如果你有兴趣打印所有元素的Numer:。然后尝试下面的代码:

all_spans = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//span[text()='Numer:']//parent::li")))
for span in all_spans:
    print(span.text)

控制台输出:

Numer: 0-35954378
Numer: 0-35259548
Numer: 0-35311342
Numer: 0-35436922
Numer: 0-35362206
Numer: 0-35707354
Numer: 0-35779189
Numer: 0-35994685
Numer: 0-35536329
Numer: 0-35945281
Numer: 0-35873282
Numer: 0-35959753
Numer: 0-35837249
Numer: 0-35776807
Numer: 0-35618989
Numer: 0-35532919
Numer: 0-35631487
Numer: 1-40989193
Numer: 1-43697413
Numer: 1-40468853
Numer: 1-45289353
Numer: 1-45289173
Numer: 1-42777553
Numer: 1-41613883
Numer: 1-72373302
Numer: 1-39444273
Numer: 1-73146322
Numer: 1-42996963
Numer: 1-38210013
Numer: 1-72783072
Numer: 1-39825163
Numer: 1-39480713
Numer: 1-41967373
Numer: 1-38702863
Numer: 1-43687943
Numer: 1-66278552
Numer: 1-35865053
Numer: 1-36381873
Numer: 1-42179823
Numer: 1-42478053
Numer: 1-40804723
Numer: 1-60160692
Numer: 1-42572953
Numer: 1-41972593
Numer: 1-71537212
Numer: 1-39706293
Numer: 1-69067952
Numer: 1-40590473
Numer: 1-42523973
Numer: 1-37934343

进程以退出代码0完成
英文:

You just have to wait until the page loads completely and desired element is visible. You can achieve that by using selenium's waits

Try this:

all_spans = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")))
for span in all_spans:
    print(span.text)

Imports required:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Console output:

Numer: 0-35954378

Process finished with exit code 0

UPDATE: In case you are interested in printing all the elements Numer:. Then try the below code:

all_spans = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//span[text()='Numer:']//parent::li")))
for span in all_spans:
    print(span.text)

Console Output:

Numer: 0-35954378
Numer: 0-35259548
Numer: 0-35311342
Numer: 0-35436922
Numer: 0-35362206
Numer: 0-35707354
Numer: 0-35779189
Numer: 0-35994685
Numer: 0-35536329
Numer: 0-35945281
Numer: 0-35873282
Numer: 0-35959753
Numer: 0-35837249
Numer: 0-35776807
Numer: 0-35618989
Numer: 0-35532919
Numer: 0-35631487
Numer: 1-40989193
Numer: 1-43697413
Numer: 1-40468853
Numer: 1-45289353
Numer: 1-45289173
Numer: 1-42777553
Numer: 1-41613883
Numer: 1-72373302
Numer: 1-39444273
Numer: 1-73146322
Numer: 1-42996963
Numer: 1-38210013
Numer: 1-72783072
Numer: 1-39825163
Numer: 1-39480713
Numer: 1-41967373
Numer: 1-38702863
Numer: 1-43687943
Numer: 1-66278552
Numer: 1-35865053
Numer: 1-36381873
Numer: 1-42179823
Numer: 1-42478053
Numer: 1-40804723
Numer: 1-60160692
Numer: 1-42572953
Numer: 1-41972593
Numer: 1-71537212
Numer: 1-39706293
Numer: 1-69067952
Numer: 1-40590473
Numer: 1-42523973
Numer: 1-37934343

Process finished with exit code 0

答案2

得分: 0

只返回翻译好的部分:

"Along with the wait, it is also a good idea to avoid as much as possible this type of xpath : div[1]//li[2]... In your case you can use the methods available for the selenium elements:
除了等待,尽量避免使用这种类型的XPath:div[1]//li[2]... 在你的情况下,你可以使用Selenium元素可用的方法:

list_res= driver.find_element(By.CLASS_NAME, 'items-row')
vehicles = list_res.find_elements(By.CLASS_NAME, 'lots-search')

check if ther is any car in the page

如果页面上没有汽车,检查一下
if len(vehicles)==0:
print("No car in the list")
print("列表中没有汽车")
for vehicle in vehicles:
item_specs = vehicle.find_element(By.CLASS_NAME, 'item-specs')
number = item_specs.find_elements(By.TAG_NAME, 'li')[0]
print(number.text)

The code is more readable and easy to debug
代码更易读且易于调试"

英文:

Along with the wait, it is also a good idea to avoid as much as possible this type of xpath : div[1]//li[2]... In your case you can use the methods available for the selenium elements:

list_res= driver.find_element(By.CLASS_NAME, 'items-row')
vehicles = list_res.find_elements(By.CLASS_NAME, 'lots-search')
# check if ther is any car in the page
if len(vehicles)==0:
    print("No car in the list")
for vehicle in vehicles:
    item_specs = vehicle.find_element(By.CLASS_NAME, 'item-specs')
    number = item_specs.find_elements(By.TAG_NAME, 'li')[0]
    print(number.text)

The code is more readable and easy to debug

huangapple
  • 本文由 发表于 2023年3月20日 23:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定