Search code examples
pythonseleniumselenium-webdriverwebdriverselenium-webdriver-python

Your requested host "localhost" could not be resolved by DNS error accessing website using Selenium Python


I am trying to use selenium on Windows. Whenever I run this code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
path = "C:/Users/User/Webdriver/chromedriver.exe"

driver = webdriver.Chrome(chrome_options=options, executable_path=path)
driver.get("http://www.python.org")

I get the following error. I don’t really understand the problem. I am not using a proxy or anything like that. Therefore I don't think it is related to my network but I could be completely wrong.

in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: <HTML><HEAD>
<TITLE>Network Error</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Network Error (dns_unresolved_hostname)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
Your requested host "localhost" could not be resolved by DNS.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">

</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>```

Solution

  • This error message...

    Your requested host "localhost" could not be resolved by DNS
    

    ...implies that as per the DNS entries localhost wasn't resolved properly.

    Ensure that hosts file have the following entries:

    127.0.0.1      localhost
    

    Incase you are behind a proxy, you may like to:

    • Remove the proxy settings temporarily.
    • Ensure localhost is resolved by DNS.
    • Set up the proxy again.

    You can find a detailed discussion in Apache: Your requested host "localhost" could not be resolved by DNS


    tl; dr

    hosts file ignored, how to troubleshoot?