![](/img/trans.png)
[英]Selenium redirects me back to the starting page when i do driver.get(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 ,但也没有看到任何答案/解决方案:
下面是对我有用的代码。
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()
请注意,我将代码结构化为classes
和methods
,只是为了提高可读性,这将有助于我调试代码。
但主要的变化是我必须明确提到我的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.