【问题标题】:python selenium driver.get() opens a blank page for google webdriver instead of the actual url in Windowspython selenium driver.get() 为 google webdriver 打开一个空白页面,而不是 Windows 中的实际 url
【发布时间】:2021-08-15 23:45:03
【问题描述】:

我有一个 Windows 10 64 位系统。

我下面的简单 selenium 代码运行时没有异常/错误,但会打开一个空白页面而不是打开 google.com,并且它与任何 url 的行为相同,不仅是 google.com

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


#Chrome browser version 91.0.4472.77
# Crome-Driver version used: 91.0.4472.19 <--latest

url='https://www.google.co.in/'
driver_path='F:\Path\chromedriver_win32\chromedriver.exe'

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--ignore-ssl-errors')
#chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path= driver_path, options= chrome_options)
driver.get(url)

我得到的输出:

Screenshot

谁能告诉我,这是怎么回事?我以前写过 selenium 代码(虽然在不同的机器上),但从来没有遇到过这样的基本问题,也不知道如何解决它,因为代码根本没有报告错误。

我确实浏览了一些其他相关问题,但也没有看到任何答案/解决方案:

Selenium webdriver loads a blank page instead of the target URL

【问题讨论】:

标签: python-3.x selenium google-chrome selenium-webdriver selenium-chromedriver


【解决方案1】:

下面是对我有用的代码。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class user_login:

    def __init__(self,path,url):

        self.path = path
        self.url = url
        self.setup = None

    def chrome_setup(self):

        try:
            options = webdriver.ChromeOptions()
            options.add_argument("start_maximized")
            options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
            self.chrome_driver = webdriver.Chrome(executable_path=self.path, options=options)
            self.chrome_driver.implicitly_wait(20)
            print("Chrome Driver Loaded")
        except Exception as e:
            print("exception is: " + str(e))

        else:
            self.setup = True
            self.load_url()


    def load_url(self):

        if self.setup is True:

            try:
                print("Loading URL")
                self.chrome_driver.get(self.url)
                
            except Exception as e:
                print(str(e))
                

if __name__ == "__main__":
    
     
    url='/path/to/my/url'
    driver_path='/path/to/my/chromedriver.exe'
    obj = user_login(driver_path,url)
    obj.chrome_setup()
             

请注意,我将代码结构化为classesmethods,只是为了提高可读性,这将有助于我调试代码。

但主要的变化是我必须明确提到我的chrome.exe 的二进制位置,即 chrome 浏览器的 exe 文件。这是代码行:options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

implicit wait 是可选的,但这是一个很好的做法,因此我添加了它:self.chrome_driver.implicitly_wait(20)。但如果没有它,也只是上面的代码更改,使代码像魅力一样工作。

【讨论】:

    【解决方案2】:

    从 selenium 开始时很容易犯错误,解决此问题的方法是在程序的前面添加 driver.get(url)

    【讨论】:

    • @himynamesis 我不明白。我的代码有你提到的那一行
    • 你这样做了,但它在最后,这意味着一旦你运行了所有代码,然后你输入 url。您需要将第一行之一作为 url,否则 selenium 将默认打开一个仅显示数据的选项卡。
    • @himynamesis 那么你在哪里建议代码中的这一行?
    • 我建议您在定义驱动程序路径后立即放置它。
    • 您收到的错误信息是什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多