簡體   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