S
S
Studentka19962021-08-28 15:00:19
Python
Studentka1996, 2021-08-28 15:00:19

Why is Selenium not finding elements (pythonanywhere)?

Everything works fine locally. Deployed the project to the server - problem:

Page title was 'Login • Instagram'
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="_5wCQW" ]//video"}
(Session info: headless chrome=78.0.3904.70)

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="KL4Bh"]//img"}
(Session info: headless chrome=78.0. 3904.70)

The problem is with Instagram, it reads other resources successfully.

What could it be? Authorization on Instagram is also successful, there is no blocking. I increased the time, set implicit waits .. nothing helps ..

Code:

def check_xpath_exists(driver, xpath_element, one_URL): #  метод проверяет по xpath существует ли элемент на странице
    try:
        driver.find_element_by_xpath(xpath_element)
        exist = True # если элемент найден
    except NoSuchElementException as e:
        print(e)
        exist = False
    return exist

chromedriver_autoinstaller.install()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless") # запуск в фоном режиме
chrome_options.add_argument("--disable-gpu")
                       
driver = webdriver.Chrome(options=chrome_options)
try:
      # # ------ тестирование веб драйвера на поиск элементов по xpath ------------
     # driver.get("https://www.google.com") # https://help.pythonanywhere.com/pages/selenium/                 
     # if check_xpath_exists(driver, "/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]", "https://www.google.com") == True:
             #  print('Найден')

     # authorize(driver)
     driver.get("https://www.instagram.com/p/CShQ6YCiX5u/")
     sleep(3)
     xpath_for_img = '//div[@class="KL4Bh"]//img' # универсальный xpath - находит все изображения в аккаунте юзера 
     xpath_for_video = '//div[@class="_5wCQW"]//video'
     if check_xpath_exists(driver, xpath_for_video, "https://www.instagram.com/p/CShQ6YCiX5u/") == True: 
           print('Найден')                
     if check_xpath_exists(driver, xpath_for_img, "https://www.instagram.com/p/CShQ6YCiX5u/") == True:
          print('Найден')
                 
 finally: # выполняет в любом случае, было ли исключение, или нет 
          # driver.close() # закрытие активной вкладки
          driver.quit() # завершение работы драйвера


instagram whitelisted - https://www.pythonanywhere.com/whitelist/
Example:
https://www.youtube.com/watch?v=hibhU5Rqt1s , https://github.com/bjcarlson42/youtube/blob/master ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
conopus, 2021-08-28
@Studentka1996

I can't say for sure, but 95% of problems with Selenium are problems with timing. For example, the element did not have time to be drawn. Try waiting to add

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question