Python Selenium opens Website but then Website closes - python

I wanna write a programm that opens a website for me and doesnt closes it right after
I tried to open a website and expected it to stay open but it didnt
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service_object_name = Service(r'C:\SneakerPython\chromedriver.exe')
options_name = webdriver.ChromeOptions()
options_name.add_argument("--start-maximized")
browser = webdriver.Chrome(service=service_object_name, options=options_name)
browser.get("https://www.solebox.com/de_DE/p/jordan-air_jordan_1_retro_high_og_%22gorge_green%22-gorge_green%2Fmetallic_silver-wht-02142227.html")

You can add the option detach to let your browser open after your code has been executed :
detach (Boolean) — whether browser is closed when the driver is sent the quit command
I have also modified your code adding ChromeDriverManager that allows to dynamically install the correct chromedriver for your machine.
Just run the following command in your terminal:
pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options_name = webdriver.ChromeOptions()
options_name.add_argument("--start-maximized")
options_name.add_experimental_option("detach", True)
browser = webdriver.Chrome(
service=Service(ChromeDriverManager().install()), options=options_name
)
browser.get("https://www.solebox.com/de_DE/p/jordan-air_jordan_1_retro_high_og_%22gorge_green%22-gorge_green%2Fmetallic_silver-wht-02142227.html")

Related

Selenium opens only Google Chrome (using webdriver-manager)

Im trying to run selenium on Brave Browser instead of Google Chrome.
As the docs indicate in (https://pypi.org/project/webdriver-manager/#use-with-edge), I should input this exactly and Brave Browser will run, except it wont at all, it will run only Google Chrome
This is the code im using:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
import time, urllib3.request
driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))
driver.get("https://www.google.com/")
time.sleep(5)
It will only run Google Chrome instead of Brave Browser, anyone could please try and help me out to run on Brave Browser using webdriver_manager?
Thanks
If you have Brave Browser installed on your computer, you can set the binary location of the webdriver.ChromeOptions to the location of brave.exe on your computer. In my case, the brave browser program is located here:
"C:\\Program Files (x86)\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
Here is an example of how to do this:
Code:
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
option = webdriver.ChromeOptions()
option.binary_location = "C:\\Program Files (x86)\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()), options=option)
driver.get("https://www.google.com")

Google chrome closes automatically after executing Python selenium code despite using the option class to keep it open

I have checked online and it was mentioned that Selenium closes the browser after running unless you use the option module or the driver.quit() or driver.close() functions but I used the option as shown in the code below but Chrome still closes after 2-3 seconds.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_driver_path = r"C:\Development\chromedriver.exe"
serv = Service(chrome_driver_path)
driver = webdriver.Chrome(service=serv, options=chrome_options)
driver.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')

Chrome opens with “Data;” with selenium chromedriver

Trying to open "Google" or any other page (website) from Chrome via selenium chrome driver in python.
The code is :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Chrome()
driver.get('https://google.com')
However, this opens my chrome window with the specified link and "data;" tab.
Why that data; tab opens? How to fix it?
Using latest versions of Chrome and Chromedriver
You don't need as much module for this just remove all of those apart from:
from selenium import webdriver
And try again you will not get another tab with congaing data.
import time
time.sleep(1)
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
time.sleep(1)
I'm not sure if its the same problem, but some time ago I made an exe script to run in another PCs and in one of the PCs selenium just didn't worked with Chrome.
This is the question I posted, but the answers didn't help me, hope it works with you: Chromedriver do not open a new session, it opens a new tab in a existing session
If it doesn't work, I made a workaround to run with Firefox instead of Chrome to ensure it would work properly.
With selenium 4 (or newer), you can use the following code to launch a new browser, (without the Chrome is being controlled message), and then the browser navigates to Google in the same tab:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService()
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://google.com')

driver.get not working when opening chrome driver with cookies

I'm trying to use selenium on OSX to open a chrome window with existing cookies so that I can bypass login. When I don't add the argument 'chrome_options=option' to open chrome with my user settings, the driver.get function works fine and it opens a chrome window with no extensions loaded and browses to the url. But when I use the code as shown below, it simply opens chrome (with all extensions loaded) but does not browse to the URL. Am I missing something simple?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/me/Library/Application Support/Google/Chrome/")
driver = webdriver.Chrome("./assets/chromedriver",chrome_options=options)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.gmail.com/")

Categories