NoSuchElementException when loading code using Selenium on Heroku

2024/10/6 16:29:37

Error:

ERROR:asyncio:Task exception was never retrieved2022-03-14T14:08:52.425684+00:00 app[worker.1]: future: <Task finished name='Task-30' coro=<Dispatcher._process_polling_updates() done, defined at /app/.heroku/python/lib/python3.9/site-packages/aiogram/dispatcher/dispatcher.py:407> exception=NoSuchElementException('no such element: Unable to locate element: {"method":"css selector","selector":".media-cards-grid"}\n  (Session info: headless chrome=99.0.4844.51)', None, ['#0 0x5586a25c17d3 <unknown>', '#1 0x5586a231d688 <unknown>', '#2 0x5586a2353c21 <unknown>', '#3 0x5586a2353de1 <unknown>', '#4 0x5586a2386d74 <unknown>', '#5 0x5586a23716dd <unknown>', '#6 0x5586a2384a0c <unknown>', '#7 0x5586a23715a3 <unknown>', '#8 0x5586a2346ddc <unknown>', '#9 0x5586a2347de5 <unknown>', '#10 0x5586a25f249d <unknown>', '#11 0x5586a260b60c <unknown>', '#12 0x5586a25f4205 <unknown>', '#13 0x5586a260bee5 <unknown>', '#14 0x5586a25e8070 <unknown>', '#15 0x5586a2627488 <unknown>', '#16 0x5586a262760c <unknown>', '#17 0x5586a2640c6d <unknown>', '#18 0x7f9745a5d609 <unknown>', ''])>

Here is the problematic part of the code:

    chrome_options = webdriver.ChromeOptions()chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")chrome_options.add_argument("--headless")chrome_options.add_argument("--disable-dev-shm-usage")chrome_options.add_argument("--no-sandbox")driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)driver.get(link)block = driver.find_element(by=By.CLASS_NAME, value='media-cards-grid')all_image = block.find_elements(by=By.CLASS_NAME, value='media-card')all_image = all_image[0:10]for image in all_image:image_link = image.get_attribute('data-src')name = image.find_element(By.CLASS_NAME, 'media-card__title').textref = image.get_attribute('href')manga = InlineKeyboardMarkup()manga.add(InlineKeyboardButton(name, url=ref))await bot.send_photo(chat_id=callback.message.chat.id, photo=image_link, reply_markup=manga)shwmrrslts = InlineKeyboardMarkup()shwmrrslts.add(InlineKeyboardButton('show more results', url=link))await callback.message.answer('if you want to see more, click on the button', reply_markup=shwmrrslts)

I searched for a solution on the internet but never found it. also, this code worked fine from my computer:

    driver = webdriver.Firefox()driver.get(link)block = driver.find_element(By.CLASS_NAME, 'media-cards-grid')all_image = block.find_elements(By.CLASS_NAME, 'media-card')all_image = all_image[0:10]# print(type(all_image))for image in all_image:image_link = image.get_attribute('data-src')name = image.find_element(By.CLASS_NAME, 'media-card__title').textref = image.get_attribute('href')manga = InlineKeyboardMarkup()manga.add(InlineKeyboardButton(name, url=ref))await bot.send_photo(chat_id=callback.message.chat.id, photo=image_link, reply_markup=manga)shwmrrslts = InlineKeyboardMarkup()shwmrrslts.add(InlineKeyboardButton('show more results', url=link))await callback.message.answer('if you want to see more, click on the button', reply_markup=shwmrrslts)

sorry for my bad english, thanks in advance sorry

Answer

This error message...

NoSuchElementException('no such element: Unable to locate element: {"method":"css selector","selector":".media-cards-grid"

...implies that the NoSuchElementException was raised when trying to locate the desired element through the following locator strategy:

(by=By.CLASS_NAME, value='media-cards-grid')

You need to construct a more canonical locator strategy which identifies the element uniquely within the DOM Tree


References

You can find a couple of relevant discussions on NoSuchElementException in:

  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
https://en.xdnf.cn/q/118930.html

Related Q&A

Python alphanumeric

Problem:I have to go through text file that has lines of strings and determine about each line if it is alphanumeric or not. If the line is alphanumeric print for example "5345m34534l is alphanume…

Python 3 - exec() Vs eval() - Expression evaluation [duplicate]

This question already has answers here:Whats the difference between eval, exec, and compile?(3 answers)Closed 7 years ago.After reading query.below python code is still not clear,>>> exec(pri…

What is the syntax for printing multiple data types in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

When scraping all the div to get the data getting the null list using lxml in python

I want to scrape the product title , product link , product price but when I am using the xpath it is showing the null list . How to add the xpath and for loop to get the above details . I have tried …

Python how to convert this for loop into a while loop [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Converting a for loop to a while loop I have this for a for loop which I made I was wondering how I would write so it woul…

Joining elements in Python list

Given a string, say s=135 and a list, say A=[1,2,3,4,5,6,7], how can I separate the values in the list that are also in s (a digit of s) from the other elements and concatenate these other elements. Th…

Python - Count letters in random strings

I have a bunch of integers which are allocated values using the random module, then converted to letters depending on their position of the alphabet.I then combine a random sample of these variables in…

Looping until a specific key is pressed [duplicate]

This question already has answers here:detect key press in python, where each iteration can take more than a couple of seconds?(4 answers)Closed 2 years ago.I was trying to make a while loop which wou…

Getting sub-dataframe after sorting and groupby

I have a dataframe dfas:Election Year Votes Vote % Party Region 0 2000 42289 29.40 Janata Dal (United) A 1 2000 27618 19.20 Rashtriya Ja…

Use .vss stencil file to generate shapes by python code (use .vdx?)

I want to have my python program generate visio drawings using shapes from a stencil (.vss) file. How can I do that? I think I could generate the xml formatted .vdx file, but there isnt a lot of docum…