Python Selenium open URL in same Firefox window - python

I am using Python Selenium to open a Firefox browser and go to a URL. The function I am using to do this is...
def openurl_function():
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.example.com')
When I run the function it always opens a new instance of FireFox, is there a way to have it to just open using the same browser instance?
Currently if I run the function 10 times then I get 10 FireFox browsers open.

Just keep reusing the same driver. You are creating a new browser, every time you call
driver = webdriver.Firefox()
Also, because you never quit() on your driver, you will probably have all the browsers stay open as orphans because you deleted the handle to them when you created a new browser.

Related

Chrome crashes when selenium launches a website

I am trying to open a website on chrome via selenium with the following code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s=Service('C:/Users/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)
link to problem: https://share.cleanshot.com/p1qu5y
This is not an issue or crash. After the specified actions are completed successfully, selenium closes the web browser. This program works fine.
Use the following code with a while True block
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
This isn't any crash or an error as such. Selenium automatically closes the client i.e. the Chrome Browser instance after executing the last line of your code. However this practice may accumulate undeleted/zombie chromedriver instances within your system.
Ideally, you always need to invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully at the end of your tests.
the chromedrive path in your code, i guess your platform is windows.
but the picture you post revease that your machine is mac.
pay attention to the chromedriver file PATH and your PC platform, make sure they suit to each other.
or you could put chromedriver in system PATH variable, so you do not need to specify the chromedrive path in your code

Selenium closes after it opens

So I'm using Python 3.9.5
Everytime I use the driver.get function, chrome appears for a second and then immediately closes the window. How do I make it like it stays open? It used to work, but when I closed VSCode and re-opened it, I did something that made it so that chrome doesn't appear to be opened.
I think you can solve this by adding the detatch option.
For example:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
Check the logs for more information. Sometimes your web browser has been updated and the chromedriver does not match with the version, so, you have to download the specific version of your browser.

How to open a url using an adress of an application

How to open a URL without webbrowser using Python, but with an address of the application with which I want to open it.
There are three ways to do this
Case 1: You just need to open a page. and that is it,
import webbrowser
webbrowser.open("https://yoursite.com")
The above will open https://yoursite.com on your default browser
Case 2: You need to open, as well as do a task eg refresh
from selenium import webdriver
import time
driver = webdriver.Edge("path to your edge driver")
driver.get("https://yoursite.com")
# The command below will refresh your webbrowser after 3 seconds of opening it
time.sleep(3)
driver.refresh()
If you want to use chrome... use driver = webdriver.Chrome("path to your chrome driver")
Case 3:You have already opened your webbrowser (via selenium) and you are in another program and you want to see it without opening it manually
from selenium import webdriver
import time
driver = webdriver.Edge("path to your edge driver")
driver.get("https://yoursite.com")
time.sleep(10)
# this command stores your webbrowser window in a variable
main_page = driver.current_window_handle
# this command moves the webbrowser to the front
driver.switch_to.window(main_page)
Also you have to install a driver for your browser if you are using the selenium thing...
For edge go to https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#:~:text=Microsoft%20Edge%20WebDriver%20for%20Microsoft%20Edge%20will%20work,find%20your%20correct%20build%20number%3A%20Launch%20Microsoft%20Edge. and choose your edge version...
And for chrome go to https://chromedriver.chromium.org/downloads
You will also have to do pip install selenium in the terminal...
Hope this helps ;)
Try selenium, although you'll have to first download Chromedriver:
from selenium import webdriver from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox() driver.get("http://stackoverflow.com/")
body = driver.find_element_by_tag_name("body") body.send_keys(Keys.CONTROL + 't')
driver.close()

Python, Selenium. Successfully initialized webdriver but couldn't open a web page because "there is no attribute called get"

I imported selenium
from selenium import webdriver
Initialized chrome webdriver as browser
browser = webdriver.chrome
But when I try to open a new tab I get a error
Error
Help?
you haven't created the browser object use below code:
from selenium import webdriver
driver =webdriver.Chrome(r"C:\Users\Downloads\chromedriver.exe")
driver.get("https://www.google.com")
you just referenced the method you should trigger and call it by passing arguments

How to check if chromedriver exist or running?

I am using 3rd party software to create a fancy application GUI with several buttons.
Each buttons will execute different .py file/.exe file. For instance:-
btnYahoo = execute yahoo.py/yahoo.exe
btnGoogle = execute google.py/google.exe
So inside both py scripts is using chromedriver to launch Chrome Browser and redirect to specific URLs
google.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://www.google.com")
yahoo.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://malaysia.yahoo.com/?p=us")
So if I execute both scripts above, it will launch 2 chrome browser.
Therefore, my concern is how can I check is webdriver.Chrome is running?
If so, then assign the webdriver.Chrome to a variable, so that I can open new tab and do further automate script progress.
For example of expected result:
Execute google.py - A new chrome browser is open and redirect to
www.google.com
Execute yahoo.py - If webdriver.Chrome is executed/existed, then assign the browser to driver variable. Else launch new browser
Thanks for advance information.
I was able to do so by checking if driver.session_id was None.

Categories

Resources