繁体   English   中英

python selenium driver.get() 为 google webdriver 打开一个空白页面,而不是 ZAEA23489CE3AAACD40B340E 中的实际 url

[英]python selenium driver.get() opens a blank page for google webdriver instead of the actual url in Windows

我有一个 Windows 10、64 位系统。

My simple selenium code below runs without exception/error, but opens a blank page instead of opening google.com and its the same behavior with any url, not only 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)

Output 我得到:

截屏

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

我通过发布的其他一些相关问题做了 go ,但也没有看到任何答案/解决方案:

Selenium webdriver加载一个空白页面而不是目标URL

下面是对我有用的代码。

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) 但是没有它,也只是上面的代码更改,使代码像魅力一样工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM