Python:Selenium Chrome Driver打开空白页面

3
运行这段简单代码时,会打开一个空白页面,URL中写着"data:,"。 Chrome驱动程序是正确的版本(ChromeDriver 81.0.4044.69),并与我的GoogleChrome版本(81.0.4044.122)相匹配。Selenium也已更新(3.141.0)。 我还将驱动程序的文件夹添加到系统的PATH中。 同样尝试了在URL中使用http而不是https。
from selenium import webdriver

class GoogleBot:
def __init__(self):
    self.driver = webdriver.Chrome(executable_path="C:\Drivers\chromedriver.exe")
    driver.get("https://www.google.es/")


GoogleBot()

Screenshot.jpeg

1个回答

2

您的代码中使用了driver而不是self.driver。请参考下面的代码来解决您的问题::

from selenium import webdriver

class GoogleBot:

    def __init__(self):
        self.driver = webdriver.Chrome(executable_path=r"path for chromedriver.exe")

    def googleTest(self):
        self.driver.get("https://www.google.es/")
        self.driver.close()

if __name__ == "__main__":
    GoogleBot = GoogleBot()
    GoogleBot.googleTest()

我尝试编写 self.driver 但是没有任何变化。我注意到当我不传递任何参数给webdriver.Chrome()时,它会打开相同的空白数据页 'data:,'。 - PythonRayson
尝试重新启动您的计算机并尝试一下,因为我认为这不是兼容性问题。 - SeleniumUser
我现在已经下载了Firefox和geckodriver。`from selenium import webdriverdriver = webdriver.Firefox(executable_path='C:\Drivers\geckodriver.exe') driver.get('https://www.google.es/') `这段代码给我一个错误,说CHROMEDRIVER不在PATH中。为什么它默认打开Chrome?这有助于我的主要问题吗? - PythonRayson
检查您是否调用了正确的程序,否则请检查您的代码是否为Chrome进行了初始化。 - SeleniumUser
已解决。我的错,当使用运行快捷方式时,我正在运行另一个Python文件。无论如何,非常感谢你,亲爱的朋友! - PythonRayson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接