我正在为一个网络抓取应用程序开发这个机器人。
import os
import booking.constanst as const
from selenium import webdriver
class Booking(webdriver.Chrome):
def __init__(self, driver_path=(r"C:UsersNarsildevseleniumDriverschromedriver.exe")):
self.driver_path = driver_path
os.environ['PATH'] += self.driver_path
super(Booking, self).__init__()
def land_page(self):
self.get(const.BASE_URL)
我收到这个错误消息:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
我试图将路径添加到我的环境变量中,设置chromedrive的路径并执行,但我仍然收到这个错误。
os.environ['PATH'] += self.driver_path
这不是设置环境变量的正确方式。实际上webdriver
允许您指定chromedriver二进制文件的路径,所以您不需要使用环境变量。
CHROMEDRIVER_PATH = r"C:UsersNarsildevseleniumDriverschromedriver.exe")
class Booking(webdriver.Chrome):
def __init__(self, driver_path=CHROMEDRIVER_PATH):
self.driver_path = driver_path
super(Booking, self).__init__(executable_path=driver_path)
此外,在大多数情况下,IMO组合比继承更适合构建页面对象。这是样品。
class BasePageObject:
def __init__(self, driver_path):
self.driver = webdriver.Chrome(executable_path=driver_path)
class Booking(BasePageObject):
def land_page(self):
self.driver.get(const.BASE_URL)