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

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

Python Selenium find_elements by xPATH returns nothing

问题

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

  1. from selenium.webdriver.common.by import By
  2. from selenium import webdriver
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. driver = webdriver.Chrome()
  6. 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')
  7. # 使用等待确保页面加载完成
  8. wait = WebDriverWait(driver, 10)
  9. element = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")))
  10. all_spans = driver.find_elements(by=By.XPATH, value="//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")
  11. for span in all_spans:
  12. 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

  1. from selenium.webdriver.common.by import By
  2. from selenium import webdriver
  3. driver=webdriver.Chrome()
  4. 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')
  5. all_spans = driver.find_elements(by=By.XPATH,value="//div[@class='row']//div[1]//div[2]//div[2]//ul[1]//li[1]")
  6. for span in all_spans:
  7. print(span.text)

What I am doing wrong ?

答案1

得分: 0

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

尝试这个:

  1. 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]")))
  2. for span in all_spans:
  3. print(span.text)

所需导入:

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

控制台输出:

  1. Numer: 0-35954378
  2. 进程以退出代码0完成

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

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

控制台输出:

  1. Numer: 0-35954378
  2. Numer: 0-35259548
  3. Numer: 0-35311342
  4. Numer: 0-35436922
  5. Numer: 0-35362206
  6. Numer: 0-35707354
  7. Numer: 0-35779189
  8. Numer: 0-35994685
  9. Numer: 0-35536329
  10. Numer: 0-35945281
  11. Numer: 0-35873282
  12. Numer: 0-35959753
  13. Numer: 0-35837249
  14. Numer: 0-35776807
  15. Numer: 0-35618989
  16. Numer: 0-35532919
  17. Numer: 0-35631487
  18. Numer: 1-40989193
  19. Numer: 1-43697413
  20. Numer: 1-40468853
  21. Numer: 1-45289353
  22. Numer: 1-45289173
  23. Numer: 1-42777553
  24. Numer: 1-41613883
  25. Numer: 1-72373302
  26. Numer: 1-39444273
  27. Numer: 1-73146322
  28. Numer: 1-42996963
  29. Numer: 1-38210013
  30. Numer: 1-72783072
  31. Numer: 1-39825163
  32. Numer: 1-39480713
  33. Numer: 1-41967373
  34. Numer: 1-38702863
  35. Numer: 1-43687943
  36. Numer: 1-66278552
  37. Numer: 1-35865053
  38. Numer: 1-36381873
  39. Numer: 1-42179823
  40. Numer: 1-42478053
  41. Numer: 1-40804723
  42. Numer: 1-60160692
  43. Numer: 1-42572953
  44. Numer: 1-41972593
  45. Numer: 1-71537212
  46. Numer: 1-39706293
  47. Numer: 1-69067952
  48. Numer: 1-40590473
  49. Numer: 1-42523973
  50. Numer: 1-37934343
  51. 进程以退出代码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:

  1. 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]")))
  2. for span in all_spans:
  3. print(span.text)

Imports required:

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

Console output:

  1. Numer: 0-35954378
  2. Process finished with exit code 0

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

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

Console Output:

  1. Numer: 0-35954378
  2. Numer: 0-35259548
  3. Numer: 0-35311342
  4. Numer: 0-35436922
  5. Numer: 0-35362206
  6. Numer: 0-35707354
  7. Numer: 0-35779189
  8. Numer: 0-35994685
  9. Numer: 0-35536329
  10. Numer: 0-35945281
  11. Numer: 0-35873282
  12. Numer: 0-35959753
  13. Numer: 0-35837249
  14. Numer: 0-35776807
  15. Numer: 0-35618989
  16. Numer: 0-35532919
  17. Numer: 0-35631487
  18. Numer: 1-40989193
  19. Numer: 1-43697413
  20. Numer: 1-40468853
  21. Numer: 1-45289353
  22. Numer: 1-45289173
  23. Numer: 1-42777553
  24. Numer: 1-41613883
  25. Numer: 1-72373302
  26. Numer: 1-39444273
  27. Numer: 1-73146322
  28. Numer: 1-42996963
  29. Numer: 1-38210013
  30. Numer: 1-72783072
  31. Numer: 1-39825163
  32. Numer: 1-39480713
  33. Numer: 1-41967373
  34. Numer: 1-38702863
  35. Numer: 1-43687943
  36. Numer: 1-66278552
  37. Numer: 1-35865053
  38. Numer: 1-36381873
  39. Numer: 1-42179823
  40. Numer: 1-42478053
  41. Numer: 1-40804723
  42. Numer: 1-60160692
  43. Numer: 1-42572953
  44. Numer: 1-41972593
  45. Numer: 1-71537212
  46. Numer: 1-39706293
  47. Numer: 1-69067952
  48. Numer: 1-40590473
  49. Numer: 1-42523973
  50. Numer: 1-37934343
  51. 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:

  1. list_res= driver.find_element(By.CLASS_NAME, 'items-row')
  2. vehicles = list_res.find_elements(By.CLASS_NAME, 'lots-search')
  3. # check if ther is any car in the page
  4. if len(vehicles)==0:
  5. print("No car in the list")
  6. for vehicle in vehicles:
  7. item_specs = vehicle.find_element(By.CLASS_NAME, 'item-specs')
  8. number = item_specs.find_elements(By.TAG_NAME, 'li')[0]
  9. 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:

确定