python selenium 属性错误:“Service”对象没有属性“process”

e5njpo68  于 2023-01-19  发布在  Python
关注(0)|答案(1)|浏览(787)

我正在尝试从我的cpanel运行一个python脚本。它使用selenium,尝试使用漂亮的soap提取html元素。下面是我的python代码

import os
from bs4 import BeautifulSoup
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(service=Service('/home/mesuivln/virtualenv/seopython/3.7/bin/chromedriver'))
driver.get ("https://www.tutorialspoint.com/about/about_careers.htm")
#content whole page in html format
s = BeautifulSoup(driver.page_source, 'html.parser')
#access to specific ul element with BeautifulSoup methods
l = s.find('ul', {'class':'toc chapters'})
#get all li elements under ul
rs = l.findAll('li')
for r in rs:
#get text of li elements
   print(r.text)

在以下文件/virtualenv/seopython/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 117, in assert_process_still_running return_code = self.process.poll()中给出错误AttributeError: 'Service' object has no attribute 'process'
下面是上述文件第117行附近的代码

def assert_process_still_running(self) -> None:
    """Check if the underlying process is still running."""
    return_code = self.process.poll()
    if return_code:
        raise WebDriverException(f"Service {self.path} unexpectedly exited. Status code was: {return_code}")

如何解决这个问题?我尝试在cpanel上运行

2w2cym1i

2w2cym1i1#

此代码中的问题在于,它试图使用webdriver.Chrome class,并将Service对象作为服务参数传递。但这不是使用webdriver.Chrome类的正确方法,因为它需要ChromeDriver可执行文件的路径作为参数,而不是Service object.
而不是这个:

driver = webdriver.Chrome(service=Service('/home/mesuivln/virtualenv/seopython/3.7/bin/chromedriver'))

您应该使用以下命令:

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
  • 注意 *:-确保在系统中安装最新版本的chrome浏览器和chrome驱动程序。

相关问题