简体   繁体   中英

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

I've a Windows 10, 64 bit system.

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 I get:

Screenshot

Can someone tell me, what's going on here? I have written selenium codes before (on different machines though), but never faced such elementary issues, and having no clue how to solve it, because of the code reporting no error at all.

I did go through some other related questions posted, but don't see any answers/solutions posted ther as well:

Selenium webdriver loads a blank page instead of the target URL

Below is the code that worked for me.

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()
             

Please note, I structured the code into classes and methods , just to increase readability which would help me debug the code.

But the main change is I had to explicitly mention the binary location to my chrome.exe ie the chrome browser's exe file. This is the code line: options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

The implicit wait is optional but a good practice, hence I added it: self.chrome_driver.implicitly_wait(20) . But without it also just the above code change, made the code work like a charm.

Super easy mistake to make when starting out with selenium, the way you would fix this is by adding driver.get(url) earlier in your program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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