Python Selenium - open more Chrome apps - selenium

I'm using Selenium to automate certain stuff in Chrome and I know how to open multiple tabs, but is it possible to open Chrome itself multiple times?
Right now, when I want to open a new Chrome app the old one closes. I want it to stay open.

every time you want to open a new Chrome Browser you have to create a new instance of the webdriver.
from time import sleep
from selenium import webdriver
fist_driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
fist_driver.get("https://google.com")
second_driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
second_driver.get("https://ifconfig.me")
sleep(5)
# using for loop
for _ in range(2): # How much browser you want to open
driver = webdriver.Chrome(executable_path="/path/to/driver")
driver.get("https://google.com")
sleep(5)

Related

Can I install chromedriver on python anywhere and not use it headless?

I am trying to use this code that works on my local machine on python anywhere and i want to understand if it is even possible:
from selenium import webdriver
from bs4 import BeautifulSoup
import time
# Initialize webdriver
driver = webdriver.Chrome(executable_path="/Users/matteo/Downloads/chromedriver")
# Navigate to website
driver.get("https://apnews.com/article/prince-harry-book-meghan-royals-4141be64bcd1521d1d5cf0f9b65e20b5")
time.sleep(5)
# Parse page source
soup = BeautifulSoup(driver.page_source, "html.parser")
# Find desired elements using Beautiful Soup
elements = soup.find_all("p")
# Print element text
for element in elements:
print(element.text)
# Close webdriver
driver.quit()
Do i need to have installed chrome to make that work or is chromium enough? Because when i run that code on my local machine a chrome page opens up. How does that work on python anywhere? Would it crush?
I am wondering if the code i am using only works if someone is on a GUI with Chrome installed or if it can work on python anywhere too.
The short answer is no. ChromeDriver must have chrome installed. You can run your tests headless for time save, but chrome still must be installed.

Open Incognito mode in selenium 4 in java

I wanted to open a normal and incognito mode together in selenium. I could open two browsers in normal mode but I am not sure how to open the another open in incognito mode in selenium 4.
The below open the 2nd browser window in normal mode where I want this to be opened in incognito mode.
driver.switchTo().newWindow(WindowType.WINDOW).get("URI");
Expected:
1st browser window in normal mode.
2nd browser window in incognito mode.
Actual:
1 browser opened in normal mode.
2nd browser opened in normal mode.
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
WebDriver driver_1 = new ChromeDriver();
driver_1.manage().window().maximize();
driver_1.get("url");
WebDriver driver_2 = new ChromeDriver(options);
driver_2.manage().window().maximize();
driver_2.get("url");
That's one of the special abilities of https://github.com/seleniumbase/SeleniumBase
Here's a test you can run with pytest after doing pip install seleniumbase:
from seleniumbase import BaseCase
class MultipleDriversTest(BaseCase):
def test_multiple_drivers(self):
self.open("data:text/html,<h1>Driver 1</h1>")
driver2 = self.get_new_driver(incognito=True)
self.open("data:text/html,<h1>Driver 2</h1>")
self.switch_to_default_driver() # Driver 1
self.highlight("h1")
self.assert_text("Driver 1", "h1")
self.switch_to_driver(driver2) # Driver 2
self.highlight("h1")
self.assert_text("Driver 2", "h1")
Driver 1 will be regular Chrome. Driver 2 will be incognito Chrome. It easily switches between the two.

Click on elements in Chrome Extension with selenium

I have been searching on the internet using Selenium (Java) interacting with Google Chrome Extension but have not been able to find an answer.
First Question
Is there a way to launch the chrome extension since Selenium only interact with WebView but not on the chrome extensions button in the browser ?
I try this method
"chrome-extension://id/index.html" but the extension did not launch as expected. I like find if there is another way to launch a chrome extension through selenium
Second Question
I am trying to click on the elements in a chrome extension with Selenium webdriver. How do I do it ? I tried the driver.CurrentWindowHandle , but it does not detect the chrome extension.
Thanks
Below is the solution with pyautogui (similar to autoit in java - so you can extend the same solution for java also).
Pre-Condition:
save the extension image in the project folder (I saved it under "autogui_ref_snaps" folder in my example with "capture_full_screenshot.png" name
Python:
Imports needed
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from Common_Methods.GenericMethods import *
import pyautogui #<== need this to click on extension
Script:
options = ChromeOptions()
options.add_argument("--load-extension=" + r"C:\Users\supputuri\AppData\Local\Google\Chrome\User Data\Default\Extensions\fdpohaocaechififmbbbbbknoalclacl\5.1_0") #<== loading unpacked extension
driver = webdriver.Chrome(
executable_path=os.path.join(chrome_options=options)
url = "https://google.com/"
driver.get(url)
# get the extension box
extn = pyautogui.locateOnScreen(os.path.join(GenericMethods.get_full_path_to_folder('autogui_ref_snaps') + "/capture_full_screenshot.png"))
# click on extension
pyautogui.click(x=extn[0],y=extn[1],clicks=1,interval=0.0,button="left")
If you are loading an extension and it's not available in incognito mode then follow my answer in here to enable it.
Try to click on extension with this JsExecutor method:
driver.execute_script("window.postMessage('clicked_browser_action', '*')")

How to make inspect window active using selenium python?

I am trying to automate a plugin in chrome browser. It requires opening an inspect window and perform operation on inspect window. I am able to open inspect window using robot class and navigate among inspect GUI elements but cannot do HTML DOM operation in inspect window. Inspect window contains iframes but I am not able to switch to those iframes from main page. When I try to list the iframes on a active page it only shows iframes from the main page not from the inspect window. I want to switch to inspect window using iframes or any other way if there is any. The code I have written so far is
from pyrobot import Robot
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.get('https://chrome.google.com/webstore/detail/dynamic-assessment-plugin/fnapgcgphlfhecijolobjodbbnjjpdga')
browser.maximize_window()
browser.implicitly_wait(15)
browser.find_element_by_css_selector("[aria-label='Add to Chrome']").click()
time.sleep(3)
robot = Robot()
robot.addExtension() # customized Robot class to add extension
robot.newtab() # customized Robto class for new tab
browser.switch_to.window(browser.window_handles[1])
browser.get('http://www.walmart.com')
time.sleep(8)
robot.inspectElement() # Customized Robot class for inspecting (Ctrl+shift+i)
While you create chromedriver object in C# this will work,
you need to provide profile by adding arguments in chromeoptions,
ChromeOptions options = new ChromeOptions();
options.AddArguments("--auto-open-devtools-for-tabs");
browser = new ChromeDriver(DrivePath, options, TimeSpan.FromSeconds(100));
You can also try to press F12 it will open inspect window
then you can use Robot class or Windows. Forms library for automation scripts manae multiple window Forms.
Your code is in python same answers are alredy provided for java and C#.
You can also refer more at:
How to open Chrome Developer console in Selenium WebDriver using JAVA
How do you automatically open the Chrome Devtools tab within Selenium (C#)?

How to use a running instance of Firefox with Selenium

I am running Ubuntu 14.04, Firefox 49.0.2, Python 3.4.3 & Selenium 3.0.1
I want to use Selenium to automate some browser functions, not to do any web site testing. How can I can I modify the simple login script below to use the instance of Firefox running on my desktop instead of opening a new Firefox window?
# login_yahoo_mail.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('http://mail.yahoo.com/?.intl=us')
enter_email = driver.find_element_by_id('login-username')
enter_email.clear()
enter_email.send_keys('cleanman2#yahoo.com')
next_button = driver.find_element_by_id('login-signin')
next_button.click()
enter_password = WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID, 'login-passwd')))
enter_password.send_keys('dumba$$yahoo!^!&')
signin_button = driver.find_element_by_id('login-signin')
signin_button.click()
Thanks, Jim
It is possible with 'Selenium 4'.
Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using:
driver.current_window_handle
driver = webdriver.Firefox()
# Store the ID of the original window
original_window = driver.current_window_handle
# Opens a new tab and switches to new tab
driver.switch_to.new_window('tab')
# Opens a new window and switches to new window
driver.switch_to.new_window('window')
#Close the tab or window
driver.close()
#Switch back to the old tab or window
driver.switch_to.window(original_window)
I haven't try it yet but since Webdriver doesn't know where the OS focus is, you can find the way you want with those options.
More: Windows and tabs handle