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

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

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]

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

This is what I tried:


creators = set()
for link in list_of_links:
    driver.get(link)
    x_path_for_followers = '//*[@id="owner-sub-count"]'
    followers = driver.find_element(By.XPATH, value=x_path_for_followers)
    nFollowers = getNumberOfFolowers(followers) # a duncion to parse from the text here to number
    if nFollowers > 500:
        continue
    x_path_for_creator = '//*[@id="text"]/a'
    creator = driver.find_element(By.XPATH, value=x_path_for_creator)
    href = creator.get_attribute("href")
    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]

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

This is what I tried:

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']

creators = set()
for link in list_of_links:
    driver.get(link)
    x_path_for_followers = '//*[@id="owner-sub-count"]'
    followers = driver.find_element(By.XPATH, value=x_path_for_followers)
    nFollowers = getNumberOfFolowers(followers) # a duncion to parse from the text here to number
    if nFollowers > 500:
        continue
    x_path_for_creator = '//*[@id="text"]/a'
    creator = driver.find_element(By.XPATH, value=x_path_for_creator)
    href = creator.get_attribute("href")
    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找到元素。

你可以尝试以下方式:

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

options = Options()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])

driver = webdriver.Chrome(options=options)

def getNumberOfFolowers(text):
    text = text.replace(' subscribers', '')
    if 'K' in text:
        num = float(text.replace('K', '')) * 1000
    elif 'M' in text:
        num = float(text.replace('M', '')) * 1000000
    else:
        num = text
    return int(num)

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']

creators = set()
for link in list_of_links:
    driver.get(link)
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[id="owner"]')))
    followers = driver.find_element(By.CSS_SELECTOR, 'div[id="owner"]').text.split('\n')[1]
    nFollowers = getNumberOfFolowers(followers)
    if nFollowers > 500:
        continue
    creator = driver.find_element(By.XPATH, '//*[@id="text"]/a').get_attribute('href')
    creator_id = creator.replace('https://www.youtube.com/', '')
    creators.add(creator_id)

print(creators)

creators:

{'@ajeet214_', '@DeltaFilmsStudio'}
英文:

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

You can try this way:

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

options = Options()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])

driver = webdriver.Chrome(options=options)

def getNumberOfFolowers(text):
    text = text.replace(' subscribers', '')
    if 'K' in text:
        num = float(text.replace('K', ''))*1000
    elif 'M' in text:
        num = float(text.replace('M', '')) * 1000000
    else:
        num = text
    return int(num)

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']

creators = set()
for link in list_of_links:
    driver.get(link)
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[id="owner"]')))
    followers = driver.find_element(By.CSS_SELECTOR, 'div[id="owner"]').text.split('\n')[1]
    nFollowers = getNumberOfFolowers(followers)
    if nFollowers > 500:
        continue
    creator = driver.find_element(By.XPATH, '//*[@id="text"]/a').get_attribute('href')
    creator_id = creator.replace('https://www.youtube.com/', '')
    creators.add(creator_id)

print(creators)

creators:

{'@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:

确定