Search code examples
pythonselenium-webdrivererror-handlingpycharm

Selenium unexpectedly closing tab without typing driver.quit(), producing errors (Using Pycharm)


I'm currently using the latest version of Selenium, and there seems to be a problem with Selenium unexpectedly closing without me using the quit method. Here's my code:

from selenium import webdriver
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()
driver.get(url="https://en.wikipedia.org/wiki/Main_Page")
article_count = driver.find_element(By.CSS_SELECTOR, "#articalcount a")
article_count.click()

And it opens the browser before closing and giving me the following error:

Stacktrace:
Backtrace:
    GetHandleVerifier [0x00007FF62C894A62+57106]
    (No symbol) [0x00007FF62C80CF52]
    (No symbol) [0x00007FF62C6DE2CB]
    (No symbol) [0x00007FF62C71786E]
    (No symbol) [0x00007FF62C71795C]
    (No symbol) [0x00007FF62C750477]
    (No symbol) [0x00007FF62C7369FF]
    (No symbol) [0x00007FF62C74E522]
    (No symbol) [0x00007FF62C736793]
    (No symbol) [0x00007FF62C70CE81]
    (No symbol) [0x00007FF62C70E064]
    GetHandleVerifier [0x00007FF62CB44222+2873042]
    GetHandleVerifier [0x00007FF62CB96590+3209792]
    GetHandleVerifier [0x00007FF62CB8F3AF+3180639]
    GetHandleVerifier [0x00007FF62C925F25+652245]
    (No symbol) [0x00007FF62C818618]
    (No symbol) [0x00007FF62C8147C4]
    (No symbol) [0x00007FF62C8148BC]
    (No symbol) [0x00007FF62C804C33]
    BaseThreadInitThunk [0x00007FFF274026AD+29]
    RtlUserThreadStart [0x00007FFF2894AA68+40]

Can someone help me with this? (Note that i was using Pycharm)

EDIT: The Output full output in the terminal was this:

Traceback (most recent call last):
  File "C:\Users\lyuan\Desktop\100_days_of_code\day48\main.py", line 7, in <module>
    article_count = driver.find_element(By.CSS_SELECTOR, "#articalcount a")
  File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 739, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute
    self.error_handler.check_response(response)
  File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#articalcount a"}
  (Session info: chrome=115.0.5790.171); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
Backtrace:
    GetHandleVerifier [0x00007FF7BB4B4A62+57106]
    (No symbol) [0x00007FF7BB42CF52]
    (No symbol) [0x00007FF7BB2FE2CB]
    (No symbol) [0x00007FF7BB33786E]
    (No symbol) [0x00007FF7BB33795C]
    (No symbol) [0x00007FF7BB370477]
    (No symbol) [0x00007FF7BB3569FF]
    (No symbol) [0x00007FF7BB36E522]
    (No symbol) [0x00007FF7BB356793]
    (No symbol) [0x00007FF7BB32CE81]
    (No symbol) [0x00007FF7BB32E064]
    GetHandleVerifier [0x00007FF7BB764222+2873042]
    GetHandleVerifier [0x00007FF7BB7B6590+3209792]
    GetHandleVerifier [0x00007FF7BB7AF3AF+3180639]
    GetHandleVerifier [0x00007FF7BB545F25+652245]
    (No symbol) [0x00007FF7BB438618]
    (No symbol) [0x00007FF7BB4347C4]
    (No symbol) [0x00007FF7BB4348BC]
    (No symbol) [0x00007FF7BB424C33]
    BaseThreadInitThunk [0x00007FFD436A26AD+29]
    RtlUserThreadStart [0x00007FFD4484AA68+40]


Solution

  • This error message...

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#articalcount a"}
    

    ...implies that no element was identified immediately within the HTML DOM using the locator strategy you have used. Hence the browser got closed.


    Solution

    To click on the article count ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      driver.get(url='https://en.wikipedia.org/wiki/Main_Page')
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='articlecount']//a"))).click()
      
    • Using XPATH:

      driver.get(url='https://en.wikipedia.org/wiki/Main_Page')
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#articlecount a"))).click()
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • Browser snapshot:

    wikipedia


    References

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