英文:
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'}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论