Jupyter - Python 3 - InvalidArgumentException while using "webdriver.Chrome()" - python

I'm trying to use webdriver.Chrome() to open the chrome browser,
Its successful but cannot access the url variable.
Chrome popup but cannot access the url
I use this Chrome driver version 92 for windows:
https://chromedriver.storage.googleapis.com/index.html?path=92.0.4515.43/
pip install selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
print('-Finish importing package')
driver = webdriver.Chrome()
url = "youtube.com"
driver.get(url)

Try to give the https/http protocol with the URL, so it should be like below.
url = "https://youtube.com"
Code
pip install selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
print('-Finish importing package')
driver = webdriver.Chrome()
url = "https://youtube.com"
driver.get(url)
#Reference

Related

Browser Close Immediately | Selenium

I start to learn selenium today. I install chrome driver and firefox driver accoding to version. I try with both chrome and firefox but browsers closes immediately. I also add drivers to PATH. I also try using direct path of driver
Chrome Version: 109.0.5414.120
Selenium Version: 4.8
Python Version: 3.8
Conda Version: 23.1.0
Code:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__) # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")
Seems like it closes, because your code is done running.
You might try:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__) # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")
input("Press ENTER to exit\n")
Service need to get the path to the chromedriver.exe as following:
service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=service)
C:\webdrivers\chromedriver.exe here is the actual path to chromedriver.exe on my computer
To keep the browser open set options with detach = true, as wollowing:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=service)
from selenium import webdriver
# Selenium Version
print(webdriver.__version__)
# Instantiating the Chrome driver
chrome_browser = webdriver.Chrome()
# Opening the browser to Google website
chrome_browser.get("https://www.google.com")

chromium webdriver can't reach the instagram login page

I am currently working on instagram login script, however i cannot even reach instagram login page with below code, is that the "chromedriver" is being blocked or any idea of my chromedriver configuration or whats wrong am I ?!!
This is my code:
# chromium-chromedrive (Not Python Library)
#!apt-get update # Update OS Files
#!apt install chromium-chromedriver
#!cp /usr/lib/chromium-browser/chromedriver /usr/bin
#!pip install selenium
from selenium import webdriver
import sys
##################################################################
# Add The System Path
##################################################################
sys.path.insert(0,'/usr/bin/chromedriver')
##################################################################
# Config The Chrome Driver Setting In Python
##################################################################
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox") #bypass OS security model
driver = webdriver.Chrome('chromedriver',options=chrome_options)
print("Loading Instagram")
driver.get("https://www.instagram.com/accounts/login/?hl=en")
print(driver.page_source)
It return "...Error Please wait a few minutes before you try again...."
I have tried to (1)remove cookies OR (2)change server IP (using Singapore / US Region IP) OR (3)even using google colab . Same result return. Anymore idea/method that I should try?
P.S. No such problem if i open instagram with my Ubuntu Chrome (With GUI).
I am able to load Instagram with the code below. Make sure you are using the latest version of chrome driver. https://chromedriver.chromium.org/downloads
#Importing selenium
import selenium
from selenium import webdriver
#chromedriver
from webdriver_manager.chrome import ChromeDriverManager
#defining driver
driver = webdriver.Chrome(ChromeDriverManager().install())
#opeining instagram
driver.get('https://www.instagram.com/accounts/login/?hl=en')

Python - InvalidArgumentException

I'm trying to start webscraping, but whenever I try to acces an URL I get an error message.
My code is the following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('www.python.org')
This opens a new Chrome window, but that's all it does.
The error message I get is the following:
InvalidArgumentException: invalid argument
(Session info: chrome=80.0.3987.149)
I work with Spyder, which I get from Anaconda, and my chromedriver.exe is in the both in the Anaconda3 folder and the Spyder folder.
Thanks in advance!
This URL is not Valid , it has to start with http://
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
try:
driver = webdriver.Chrome()
driver.get('http://www.python.org')
except Exception as e:
print(e)
finally:
if driver is not None :
driver.close()
Please include executable path and try below solution:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get("https://www.python.org")
Your url is not correct www.python.org , correct url : https://www.python.org

How to download a HTML webpage using Selenium with python?

I want to download a webpage using selenium with python. using the following code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--save-page-as-mhtml')
d = DesiredCapabilities.CHROME
driver = webdriver.Chrome()
driver.get("http://www.yahoo.com")
saveas = ActionChains(driver).key_down(Keys.CONTROL)\
.key_down('s').key_up(Keys.CONTROL).key_up('s')
saveas.perform()
print("done")
However the above code isnt working. I am using windows 7.
Is there any by which i can bring up the 'Save as" Dialog box?
Thanks
Karan
You can use below code to download page HTML:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.yahoo.com")
with open("/path/to/page_source.html", "w", encoding='utf-8') as f:
f.write(driver.page_source)
Just replace "/path/to/page_source.html" with desirable path to file and file name
Update
If you need to get complete page source (including CSS, JS, ...), you can use following solution:
pip install pyahk # from command line
Python code:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import ahk
firefox = FirefoxBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")
from selenium import webdriver
driver = web.Firefox(firefox_binary=firefox)
driver.get("http://www.yahoo.com")
ahk.start()
ahk.ready()
ahk.execute("Send,^s")
ahk.execute("WinWaitActive, Save As,,2")
ahk.execute("WinActivate, Save As")
ahk.execute("Send, C:\\path\\to\\file.htm")
ahk.execute("Send, {Enter}")

Python - Selenium Fail

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = 'C:\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
I tried running this in CMD, and I get the following error:

Categories