我在运行以下代码时遇到异常

2024-04-25 09:33:15 发布

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

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

driver=webdriver.chrome(executable_path="C:\Driver\chromedriver_win32\chromedriver.exe")
driver.get("www.youtube.com")

print (driver.title)

driver.close()

对于上面的代码,我得到了错误

TypeError: 'module' object is not callable

Tags: pathfromimportdriverseleniumcommonkeyschrome
1条回答
网友
1楼 · 发布于 2024-04-25 09:33:15

我推荐模块化编程

设置一个返回chrome驱动程序的函数,如下所示:

# Opens chrome driver
def openChrome():

    # directory to chromedrive
    chromeDriver = "C:\Driver\chromedriver_win32\chromedriver.exe"
    return webdriver.Chrome(chromeDriver)

按如下方式调用此函数:

url = "https://stackoverflow.com/"
driver = openChrome()
driver.get(url) 

因此,将来如果驱动程序不工作,您不必在每个脚本上都更改它,您可以在一个地方(函数)更改它

相关问题 更多 >