Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNicole Buckenwolf
8,721 PointsHelp with an error
When I run this exact code I get the following:
/Users/nbuckenwolf/PycharmProjects/pythonProject/.venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
warnings.warn(
Traceback (most recent call last):
File "/Users/nbuckenwolf/PycharmProjects/pythonProject/horse_test_selenium.py", line 12, in <module>
page_html = driver.page_source
File "/Users/nbuckenwolf/PycharmProjects/pythonProject/.venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 448, in page_source
return self.execute(Command.GET_PAGE_SOURCE)["value"]
File "/Users/nbuckenwolf/PycharmProjects/pythonProject/.venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "/Users/nbuckenwolf/PycharmProjects/pythonProject/.venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=123.0.6312.123)
Stacktrace:
0 chromedriver 0x0000000104a484a4 chromedriver + 4326564
1 chromedriver 0x0000000104a4096c chromedriver + 4295020
2 chromedriver 0x000000010466c088 chromedriver + 278664
3 chromedriver 0x0000000104647270 chromedriver + 127600
4 chromedriver 0x00000001046d39ac chromedriver + 702892
5 chromedriver 0x00000001046e6c0c chromedriver + 781324
6 chromedriver 0x00000001046a34e4 chromedriver + 505060
7 chromedriver 0x00000001046a3f5c chromedriver + 507740
8 chromedriver 0x0000000104a0ba40 chromedriver + 4078144
9 chromedriver 0x0000000104a107f8 chromedriver + 4098040
10 chromedriver 0x00000001049f25e4 chromedriver + 3974628
11 chromedriver 0x0000000104a11110 chromedriver + 4100368
12 chromedriver 0x00000001049e3bd4 chromedriver + 3914708
13 chromedriver 0x0000000104a31718 chromedriver + 4232984
14 chromedriver 0x0000000104a31894 chromedriver + 4233364
15 chromedriver 0x0000000104a405e0 chromedriver + 4294112
16 libsystem_pthread.dylib 0x000000019ba5af94 _pthread_start + 136
17 libsystem_pthread.dylib 0x000000019ba55d34 thread_start + 8
Process finished with exit code 1
It also opens the webpage in a new window. In case this is relevant - I already have a tab with it open, but interestingly it opens the new window in another Chrome profile that I do not currently have any windows open in. Not sure what is happening, can anyone advise? I'm using the same code as in the video, below for reference:
from bs4 import BeautifulSoup
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://treehouse-projects.github.io/horse-land/index.html')
time.sleep(5)
page_html = driver.page_source
soup = BeautifulSoup(page_html, 'html.parser')
print(soup.prettify())
driver.close()
2 Answers
Rachel Johnson
Treehouse TeacherHey Nicole Buckenwolf , thanks for your question!
When we use driver.get()
, Selenium opens a web browser (or "webdriver") to perform actions in. In our case, because we defined webdriver.Chrome()
, a Chrome browser will be opened. This will happen whether or not you already have the website open in another Chrome tab or browser.
As for the error. This will usually happen if the browser window opened by Selenium is closed by the user before anything was actioned. In this case, it's quite easy to do, because the browser window stays open for 5 seconds. After all, that's how long we've told Python to "sleep" for so the page can load. If we close this window before the rest of the code is actioned (everything past time.sleep(5)
), it'll error because there is no longer a page to look at.
If you let Selenium open its own browser window and hang out for 5 seconds, it'll close by itself and the results will show in your terminal!
I hope this helps!
Nicole Buckenwolf
8,721 Pointsthank you for this thorough explanation!