Ubuntu上的Chromedriver:selenium.common.exceptions异常.SessionNotCreatedException:消息:未创建会话

2024-04-25 14:43:05 发布

您现在位置:Python中文网/ 问答频道 /正文

在AWS(EC2实例)的ubuntu环境中遇到selenium+chromedriver问题。你知道吗

我使用的是chromedriver Linux64版本(wnload chromedriver for Linux: wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip)。然后我将chromedriver放在/usr/bin。你知道吗

Chrome是用sudo dpkg -i google-chrome-stable_current_amd64.deb为ubuntu下载的,如果我用google-chrome --version验证Chrome的版本,我会看到它是:

Google Chrome 78.0.3904.70 

下面的python代码可以工作,但问题是它只能偶尔工作。你知道吗

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1420,1080')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")
options.add_argument('--disable-gpu')


driver = webdriver.Chrome(chrome_options=options)

#Set base url (SAN FRANCISCO)
base_url = 'https://www.bandsintown.com/en/c/san-francisco-ca?page='

#Build events array 
events = []
eventContainerBucket = []

for i in range(1,2):

    #cycle through pages in range
    driver.get(base_url + str(i))
    pageURL = base_url + str(i)
    print(pageURL)

虽然上面的代码在过去可以正常工作,但如果我运行几次,最终会出现以下错误:

    Traceback (most recent call last):
  File "BandsInTown_Scraper_SF.py", line 84, in <module>
    driver = webdriver.Chrome(chrome_options=options)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
  (Session info: headless chrome=78.0.3904.70)

我已经读到要解决这个问题,您可能需要编辑etc/hosts文件。我看了这里,一切看起来都很好:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 

我也可以通过服务器使用请求和访问url。例如,以下文本没有给我任何问题:

url = 'https://www.bandsintown.com/en/c/san-francisco-ca?page=6'
res = requests.get(url)
html_page = res.content

soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
print(text)

另一个重要的信息,我相信可能会导致这个问题,是chromedriver可能不允许运行在无头模式。例如,如果我在terminal中键入chromedriver,我将得到以下消息:

Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

最后,如果我试着在/usr/bin中做chmod 777,它会说operation not permitted。这可能是问题的一部分吗?你知道吗

所以,chrome+chromedriver是同一个版本,所以这不是问题所在。铬和硒似乎被阻断了。我对如何解决这个问题有点困惑。如果有人有什么建议,我将不胜感激!非常感谢。你知道吗


Tags: inpyaddurlubuntulocalseleniumline