How to open a specific link in chrome using python - python

Though the default browser is IE or Firefox, when I run the program, I want the website to be opened in chrome.
At first, I tried this out--
import webbrowser
import os
path = "C:\Program Files (x86)\Google\Chrome\Application %s"
webbrowser.get(path).open("youtube.com")
But the webpage opened in Ie as my default browser is IE.
Then I tried the below code-
from selenium import webdriver
browser = webdriver.Chrome()
browser.open('https://directory.corp.intranet/cmsviewer/login.html')
But received many errors. Please help me out!!!

Hello and welcome to Stack overflow
try to do this :
import webbrowser
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open('http://docs.python.org/')
good luck

Try maybe the following:
import webbrowser
webbrowser.get('chrome').open("youtube.com")
As it seems that chrome is already registered by default:
https://docs.python.org/3.8/library/webbrowser.html#webbrowser.register

Your selenium code is very likely not working due to the lack web extensions like .net, .com,.org, .aspx etc. So your selenium code should work if it looked like
from selenium import webdriver
browser = webdriver.Chrome()
browser.open('https://directory.corp.intranet.aspx')

Related

Selenium python - How to load Default Chrome Profile

I need to load my own Default chrome profile where all my saved login info is, instead of selenium logging me into my websites.
I have taken this code from stackoverflow, cannot find the exact user. My issue with this code is selenium opens chrome but does not load the correct profile. Every time I open run the code it creates a new "scoped_dir" folder and runs the profile "default" from there chrome version
(C:\Users\farja\AppData\Local\Temp\scoped_dir[bunch of numbers]\Default).
I have tried closing chrome and then running my code which doesn't work
I am thinking there is a big flaw in my code but do not know what it is or how to find it. A relevant answer for 2022 would be very very much appreciated as I have literally been stuck on this for a week now and have tried multiple answers on stackoverflow, the web and youtube but nearly all give me a deprecation error.
Thank you for taking the time to read.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
ser = Service(r'C:\Users\farja\Documents\Instagram Programmes\Scheduler 2\chromedriver')
op = webdriver.ChromeOptions()
s = webdriver.Chrome(service=ser, options=op)
op.add_argument(r"--user-data-dir=C:\\Users\\farja\\AppData\\Local\\Google\\Chrome\\User Data")
op.add_argument("--profile-directory=Default")
s.get("chrome://version/")
As your usecase is to load the Default Chrome Profile you can use the argument user-data-dir and remove the argument --profile-directory as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
op = webdriver.ChromeOptions()
op.add_argument(r"--user-data-dir=C:\\Users\\farja\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
ser = Service(r'C:\Users\farja\Documents\Instagram Programmes\Scheduler 2\chromedriver')
s = webdriver.Chrome(service=ser, options=op)
s.get("chrome://version/")
References
You can find a couple of relevant detailed discussions in:
How to open a Chrome Profile through Python
How to open a Chrome Profile through --user-data-dir argument of Selenium

Python Webbrowser Library not working with Chrome

I'm trying to code an automation function which opens the entered URL. The code works if I'm not working with Chrome browser; but doesn't work with Chrome browser: it fails to detect my Chrome Browser and returns the error "could not find runnable browser".
Code:
import webbrowser
def visitwebsite():
url = input('Enter url: ')
webbrowser.get('chrome').open_new_tab(url)
Tried already 'chrome' - 'google-chrome' and redirecting it directly to the path. Maybe i did it wrong
The code is simple. You need to install selenium for this.
from selenium import webdriver
driver = webdriver.Chrome(executable_path='path to chromedriver.exe')
driver.get('https://www.google.com)

Python disable Chrome Extension

I try to use Python to log in a web, type in use id and password, and download some files. however, before logging the page, there is a error msg says
"Failed to load extension from
C:\User\xyz\AppData\Local\Temp\scoped_dir12188_17478\internal. Loading
of unpacked extensions is disabled by administrator"
. After I hit "Enter", the rest goes well.
I have searched here, and find one post is related to my question, Here. But the code is still not working for me. the pop up error windows is still showing up. Any ideas or suggestion is highly welcome. Thank you!
Here is my code:
import time
import urllib3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
driverpath = 'C:\\Users\\xyz\\AppData\\Local\\Programs\\Python\\Python36\\chromedriver.exe'
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
browser = webdriver.Chrome(executable_path=driverpath, chrome_options=chrome_options)
url ='https://userid:password#weblink'
browser.get(url)

Open Chrome but just get data in URL address want it to be google.com

This is the code I use:
import time
from selenium import webdriver
driver = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver') # Optional argument, if not specified will search path.
driver.navigate().to("http://www.google.com")
The URL I get in Google is data; I have selenium installed and Chromium. I don't know if I missed something. I tried with get driver a lot and it doesn't work.
You need to use
driver.get(url)

Python: Selenium cannot create instance of Firefox WebDriver on OS

I'm trying to scrape some data from a URL with dynamic content and learned Selenium can do the task.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/Applications/Firefox.app/Contents/MacOS')
driver = webdriver.Firefox(firefox_binary=binary)
The 4 lines above gives me
OSError: [Errno 13] Permission denied
I googled and seems that others have encountered similar issues but none of the solutions I found work. Some seem to be for Windows and others seem to be for Java rather than Python.
When running your code, I got the same error. But, if you just want to use selenium to open up Firefox browser and go from there, simply use this:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.google.com")
It will trigger an initial web browser and open up a webpage.
The following line causes the problem:
binary = FirefoxBinary('/Applications/Firefox.app/Contents/MacOS')
Make sure to provide the correct path of the binary on the filesystem, not in your application launcher, i.e.
binary = FirefoxBinary('/usr/bin/firefox')

Categories

Resources