Message: no such element: 无法定位元素 (Python, Selenium)

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

Message: no such element: Unable to locate element (Python, Selenium)

问题

I have list_of_links which is containing 2 videos that are having creators with less the 500 subscribers and one video that is having a huge number of subscribers. My task in here is to get only those creators that are having less then 500 subscribers I tried it like this, but it is giving me an error with this [tag:Traceback]

  1. 5 driver.get(link)
  2. 6 x_path_for_followers = '//*[@id="owner-sub-count"]'
  3. ----> 7 followers = driver.find_element(By.XPATH, value=x_path_for_followers)
  4. 8 nFollowers = getNumberOfFolowers(followers) # a duncion to parse from the text here to number
  5. 9 if nFollowers > 500:

This is what I tried:

  1. creators = set()
  2. for link in list_of_links:
  3. driver.get(link)
  4. x_path_for_followers = '//*[@id="owner-sub-count"]'
  5. followers = driver.find_element(By.XPATH, value=x_path_for_followers)
  6. nFollowers = getNumberOfFolowers(followers) # a duncion to parse from the text here to number
  7. if nFollowers > 500:
  8. continue
  9. x_path_for_creator = '//*[@id="text"]/a'
  10. creator = driver.find_element(By.XPATH, value=x_path_for_creator)
  11. href = creator.get_attribute("href")
  12. creators.append(href)

I want to get these 2 creators (they are creators of 2 first videos and they have less then 500 suscribers): @translationalbiology5464, @imjustslightlybetter

英文:

I have list_of_links which is containing 2 videos that are having creators with less the 500 subscribers and one video that is having a huge number of subscribers. My task in here is to get only those creators that are having less then 500 subscribers I tried it like this, but it is giving me an error with this [tag:Traceback]

  1. NoSuchElementException Traceback (most recent call last)
  2. ~\AppData\Local\Temp\ipykernel_1826454802536.py in <module>
  3. 5 driver.get(link)
  4. 6 x_path_for_followers = '//*[@id="owner-sub-count"]'
  5. ----> 7 followers = driver.find_element(By.XPATH, value=x_path_for_followers)
  6. 8 nFollowers = getNumberOfFolowers(followers) # a duncion to parse from the text here to number
  7. 9 if nFollowers > 500:

This is what I tried:

  1. list_of_links = ['https://www.youtube.com/watch?v=rXUW-BfPNx8', 'https://www.youtube.com/watchv=b9s_xrycvGA', 'https://www.youtube.com/watch?v=k9TUPpGqYTo']
  2. creators = set()
  3. for link in list_of_links:
  4. driver.get(link)
  5. x_path_for_followers = '//*[@id="owner-sub-count"]'
  6. followers = driver.find_element(By.XPATH, value=x_path_for_followers)
  7. nFollowers = getNumberOfFolowers(followers) # a duncion to parse from the text here to number
  8. if nFollowers > 500:
  9. continue
  10. x_path_for_creator = '//*[@id="text"]/a'
  11. creator = driver.find_element(By.XPATH, value=x_path_for_creator)
  12. href = creator.get_attribute("href")
  13. creators.append(href)

I want to get these 2 creators (they are creators of 2 first videos and they have less then 500 suscribers): @translationalbiology5464, @imjustslightlybetter

答案1

得分: 0

以下是翻译好的内容:

如错误明确指出,无法通过给定的XPath找到元素。

你可以尝试以下方式:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.chrome.options import Options
  6. options = Options()
  7. options.add_argument("--start-maximized")
  8. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  9. driver = webdriver.Chrome(options=options)
  10. def getNumberOfFolowers(text):
  11. text = text.replace(' subscribers', '')
  12. if 'K' in text:
  13. num = float(text.replace('K', '')) * 1000
  14. elif 'M' in text:
  15. num = float(text.replace('M', '')) * 1000000
  16. else:
  17. num = text
  18. return int(num)
  19. list_of_links = ['https://www.youtube.com/watch?v=iBk60Pv_BMo', 'https://www.youtube.com/watch?v=Cnwt5emWaOg', 'https://www.youtube.com/watch?v=OBXtdBnEUvo', 'https://www.youtube.com/watch?v=HZ8uXq5VG2w', 'https://www.youtube.com/watch?v=03c8M6LZR_k']
  20. creators = set()
  21. for link in list_of_links:
  22. driver.get(link)
  23. WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[id="owner"]')))
  24. followers = driver.find_element(By.CSS_SELECTOR, 'div[id="owner"]').text.split('\n')[1]
  25. nFollowers = getNumberOfFolowers(followers)
  26. if nFollowers > 500:
  27. continue
  28. creator = driver.find_element(By.XPATH, '//*[@id="text"]/a').get_attribute('href')
  29. creator_id = creator.replace('https://www.youtube.com/', '')
  30. creators.add(creator_id)
  31. print(creators)
  32. creators:
  33. {'@ajeet214_', '@DeltaFilmsStudio'}
英文:

As the error clearly indicates, it's unable to find the element by the given XPath.

You can try this way:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.chrome.options import Options
  6. options = Options()
  7. options.add_argument("--start-maximized")
  8. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  9. driver = webdriver.Chrome(options=options)
  10. def getNumberOfFolowers(text):
  11. text = text.replace(' subscribers', '')
  12. if 'K' in text:
  13. num = float(text.replace('K', ''))*1000
  14. elif 'M' in text:
  15. num = float(text.replace('M', '')) * 1000000
  16. else:
  17. num = text
  18. return int(num)
  19. list_of_links = ['https://www.youtube.com/watch?v=iBk60Pv_BMo', 'https://www.youtube.com/watch?v=Cnwt5emWaOg', 'https://www.youtube.com/watch?v=OBXtdBnEUvo', 'https://www.youtube.com/watch?v=HZ8uXq5VG2w', 'https://www.youtube.com/watch?v=03c8M6LZR_k']
  20. creators = set()
  21. for link in list_of_links:
  22. driver.get(link)
  23. WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[id="owner"]')))
  24. followers = driver.find_element(By.CSS_SELECTOR, 'div[id="owner"]').text.split('\n')[1]
  25. nFollowers = getNumberOfFolowers(followers)
  26. if nFollowers > 500:
  27. continue
  28. creator = driver.find_element(By.XPATH, '//*[@id="text"]/a').get_attribute('href')
  29. creator_id = creator.replace('https://www.youtube.com/', '')
  30. creators.add(creator_id)
  31. print(creators)

creators:

  1. {'@ajeet214_', '@DeltaFilmsStudio'}

huangapple
  • 本文由 发表于 2023年4月6日 19:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948831.html
匿名

发表评论

匿名网友

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

确定