Issues with opening browser window in full screen using Selenium WebDriver Chrome - selenium

I am currently doing a project where a RFID tag is tapped, and the associated webpage opens on Chrome (using Selenium). I used Selenium because I wanted to ensure that each webpage that was opened would just open on the same tab, so I wouldn't have multiple tabs open at any one time. I am now wanting when the code is run, for all webpages to be opened in full screen mode (without the search bar).
My code is as follows - I am using "driver.fullscreen_window()" as the code to open it fullscreen . Currently the tester Facebook webpage, will start with a maximised full screen, and will immediately turn back into a half screen with the search bar. Therefore, I was wondering if anyone had any ideas. I am a beginner, so any help would be fabulous.
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import subprocess
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.fullscreen_window()
link1="http://facebook.com.au"
link2="http://netflix.com.au/"
link3="http://google.com.au"
reader = SimpleMFRC522()
last_id=None
driver.get(link1)
while True:
print("Place tag")
print (id)
id,text=reader.read()
if last_id == id:
pass
else:
if id == 397491194568:
driver.get(link2)
elif id == 769847466731:
driver.get(link3)
last_id = id

Instead of driver.fullscreen_window() try passing start-maximized argument to driver options, as following:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)

After launching the URL, make the window as fullscreen_window()
driver.maximize_window()
driver.get(link1)
driver.fullscreen_window()

Related

How to click on devtools console tab with Selenium and python

I have this code:
async def acess_all():
def acess_localhost():
option = Options()
option.add_argument("--no-sandbox")
option.debugger_Address="127.0.0.1:8081"
driver = webdriver.Chrome(options=option)
driver.get("http://127.0.0.1:8081")
wait = WebDriverWait(driver, 5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.item'))).click()
try:
t = threading.Thread(target=get_infos)
t.start()
os.system("chromium --remote-debugging-port=8081 https://google.com")
except:
print("Error!")
What I need:
Guys, as you can see, this code opens the Chrome browser on the Google page, and my Selenium code opens the browser on localhost because it's accessing where the remote-debbuging address points to, but I can't access the console tab of devtools, I need to access this tab using Selenium and run a javascript command to copy the cookie in json format, but I can't, how can I access the devtools console tab?
I believe I've figured out a way to run a javascript command in the console and get a return value (using selenium, as in the question).
Note: I'm running this on a Windows computer, but the idea should remain the same on a different operating system. You might have to tweak a few things in this code. Also, all Chrome sessions have to be closed beforehand to get this to work.
Ok, unless I interpreted your question wrong (tell me if I did), this is the basic order of the things you want you want to happen when:
Open the regular chrome browser (by regular, I mean not selenium, but regular chrome.exe or Google Chrome.app) to google.com and set the debugging port (which I assume is what you're doing when you run this command: chromium --remote-debugging-port=8081 https://google.com)
Open Chromedriver (Selenium) and go to the locally-hosted debugger window at 127.0.0.1:8081
Select the "Google" window option in the list of available windows at 127.0.0.1:8081
Once the devtools window opens, move to the Console tab
Finally, run some Javascript in the Console's input box and somehow get a return value for a cookie
You already did the first few items (1-3) in your code but you needed help figuring out the rest. I think I've found a way to do it.
So assuming that you already opened the google.com window on the Chrome browser and the 127.0.0.1:8081 window on localhost, all you need to do now is access the Console.
Here's what my chromedriver (selenium) browser screen looks like at this point, just for reference.
We'll start by waiting until a specific element (devtools-elements-breadcrumbs) has loaded on the page. We wait for this so we are sure that the page is loaded enough to navigate. I found this element by looking at the driver page_source. Here's how we wait for it:
wait.until(EC.presence_of_element_located((By.TAG_NAME, "devtools-elements-breadcrumbs")))
Once the breadcrumb element is located we can move to the console window by sending a shortcut to the chromedriver browser telling it to move right one tab in the Inspector window (to Console). The command is Control + ] (Windows/Linux) or Command + ] (Mac), to go to the next panel. This is how we send that shortcut to the selenium window (once again, using Windows):
click_console = ActionChains(driver)
click_console.key_down(Keys.CONTROL).send_keys(']').key_up(Keys.CONTROL).perform()
or on a Mac,
click_console = ActionChains(driver)
click_console.key_down(Keys.COMMAND).send_keys(']').key_up(Keys.COMMAND).perform()
After moving to the Console, we now have to wait for the console area to load, so we wait for all the CodeMirror class elements (once again, found using driver.page_source)
# wait for console area to open
wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "CodeMirror")))
Now the last thing to do is to send the javascript command to the console. In this example, I'm searching for the APISID cookie.
cookie_name = "SEARCH_SAMESITE"
# javascript find the cookie
# https://stackoverflow.com/a/59603055/11073064
js_command = '(\"; \" + document.cookie).split(\"; {}\").pop().split(\';\').shift();'.format(cookie_name)
# send the command to the selenium webbrowser
send_js_command = ActionChains(driver)
send_js_command.send_keys(js_command, Keys.ENTER).perform()
and to get the value outputted in the console after you run that command:
# wait for return value span tag to be found
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'span.object-value-string.source-code')))
value = element.text
driver.close()
Here's the full code I used (on Windows).
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import threading
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import os
def start_chrome_browser_with_debug():
# this is the path to your regular google chrome browser, not selenium chromedriver
# note the chrome.exe, not chromedriver.exe.
# point the path to your regular chrome browser
os.system('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=8081 https://google.com')
cookie_name = "SEARCH_SAMESITE"
js_command = '(\"; \" + document.cookie).split(\"; {}\").pop().split(\';\').shift();'.format(cookie_name)
chrome_browser = threading.Thread(target=start_chrome_browser_with_debug)
chrome_browser.start()
option = Options()
option.add_argument("--no-sandbox")
option.debugger_Address = "127.0.0.1:8081"
driver = webdriver.Chrome(options=option)
driver.get("http://127.0.0.1:8081")
wait = WebDriverWait(driver, 5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.item'))).click()
wait.until(EC.presence_of_element_located((By.TAG_NAME, "devtools-elements-breadcrumbs")))
click_console = ActionChains(driver)
click_console.key_down(Keys.CONTROL).send_keys(']').key_up(Keys.CONTROL).perform()
wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "CodeMirror")))
send_js_command = ActionChains(driver)
send_js_command.send_keys(js_command, Keys.ENTER).perform()
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'span.object-value-string.source-code')))
value = element.text
driver.close()
os.system('taskkill /F /IM chrome.exe /T')
print(value)
Actually, you can execute the javascript command by selenium itself :
driver.execute_script("some javascript code here");
You can't use Selenium to interact with the dev tools console, however you don't need it to get the cookies, just use get_cookies() from the webdriver and convert it to json using json.dumps()
import json
cookies = driver.get_cookies()
js = json.dumps(cookies)
I couldn't make this work on a Mac using only Python and Selenium. However, I found a solution that works if I use pyautogui in addition.
I'm working on a file, duals7.svg, saved locally. I want to be able to open it, play with it and watch what happens at the Javascript developer console in Chrome.
The script below does exactly what I want:
# chrome.py
# https://www.browserstack.com/guide/python-selenium-to-run-web-automation-test
# https://www.tutorialspoint.com/how-do-i-pass-options-to-the-selenium-chrome-driver-using-python
# https://stackoverflow.com/questions/36311191/how-to-open-chrome-developer-console-in-selenium-webdriver-using-java
# https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
import pyautogui
op = webdriver.ChromeOptions ()
op.add_argument ('--allow-file-access-from-files')
op.add_argument ('--auto-open-devtools-for-tabs')
driver = webdriver.Chrome (options=op)
driver.get ('file:///Users/murray/projects/warp/dual7.svg')
console = (850, 175)
pyautogui.moveTo (console [0], console [1], duration = 0.5)
pyautogui.click (console [0], console [1])
(I don't think the pyautogui.moveTo command is needed. Including it allows me to watch the mouse move, which I like.)
When I run it (python chrome.py), I see my svg "app" in Chrome with the Javascript dev console open:
The coordinates of the console button were determined by trial and error. Please forgive the aesthetics of the app: it'll look nice when it's done.
When I interact with my "app" I see my console.log () debugging messages.
If you're interested, the app is being modified from
/* elliptic-mesh.js
*
* generates a structured mesh bounded by four splines by
* solving the elliptic Laplace equation
*
* For more info see:
* http://www.particleincell.com/2012/online-meshing-example/
* http://www.particleincell.com/2012/bezier-splines/
*
* Lubos Brieda, Particle In Cell Consulting LLC, 2012
* you may freely use this algorithm in your codes but whenever possible
* please include a link/reference to the source article
*/
My version generalizes Dr. Brieda's code a bit, adds some debugging statements, and will add save and load options.

One last hurdle with running Tor contolled by Python Selenium

I use the following code to run Tor controlled by Selenium in Lubuntu:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
torexe = os.popen(r'/home/sergey/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/firefox')
profile = FirefoxProfile(r"/home/sergey/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default")
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'/usr/local/bin/geckodriver')
driver.get("https://www.google.com")
...
It works, but in somewhat strange manner. Running it results in opening two windows, one window is Tor's while the other one being that of Firefox. Selenium controls the FF window only. The Tor window just sits there idly.
It is not to say that makes life unbearable, as I have said every thing works, but I am merely curious to know how to «make it completely right» (by this, I mean executing Selenium script in the only window of Tor).
I figured it out. In order to launch Tor using Selenium please run the following code (it still launches Firefox, this time without any Tor windows, but it uses the Tor service):
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver = webdriver.Firefox(profile)
You may want to add the following three lines to verify the Tor service provides new IP for you Firefox instance:
driver.get("http://icanhazip.com")
current_IP = driver.find_element_by_css_selector("body > pre:nth-child(1)")
print(current_IP.get_attribute("innerHTML"))
This site icanhazip.com allows one to see his external IP without necessity to pass the abominable captcha test.

How to minimize the browser window in Selenium WebDriver 3

After maximizing the window by driver.manage().window().maximize();, how do I minimize the browser window in Selenium WebDriver with Java?
Selenium's Java client doesn't have a built-in method to minimize the browsing context.
However, as the default/common practice is to open the browser in maximized mode, while Test Execution is in progress minimizing the browser would be against the best practices as Selenium may lose the focus over the browsing context and an exception may raise during the test execution. However, Selenium's Python client does have a minimize_window() method which eventually pushes the Chrome browsing context effectively to the background.
Sample code
Python:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()
Java:
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
There seems to be a minimize function now:
From the documentation on:
webdriver.manage().window()
this.minimize() → Promise<undefined>
Minimizes the current window. The exact behavior of this command is specific to individual window managers, but typicallly involves hiding the window in the system tray.
Parameters
None.
Returns
Promise<undefined>
A promise that will be resolved when the command has completed.
So the code should be:
webdriver.manage().window().minimize()
At least in JavaScript.
Unfortunately, Selenium does not provide any built-in function for minimizing the browser window. There is only the function for maximizing the window. But there is some workaround for doing this.
driver.manage().window().setPosition(new Point(-2000, 0));
Use the following code to the minimize browser window. It worked for me and I am using Selenium 3.5:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_N);
I'm using Selenium WebDriver (Java) 3.4.0 and have not found the minimize function either. Then I'm using the following code.
driver.manage().window().maximize();
driver.manage().window().setPosition(new Point(0, -2000));
We need to import the following for the Point function.
import org.openqa.selenium.Point;
For C# , tested using Selinium 3.14 ver ( Package1 , Package2) and it work
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Minimize();
In Selenium version 4, there is a method called minimize() added in Window interface (An inner interface of WebDriver) with Java.
Hence now, if you have Selenium version 4, then you can use driver.manage().window().minimize().
This works for me in Python.
The window will open and after loading it will minimize.
driver.get(url)
driver.minimize_window()
Try this. It should work.
Dimension n = new Dimension(360, 592);
driver.manage().window().setSize(n);

Selenium and Firefox on Ubuntu: focusing to the new tab

I am working on Selenium and Firefox in Ubuntu14.04. I created a new tab by send_keys(Keys.CONTROL + 't'), but keep giving commands on the first tab, maybe because I didn't focus on the new tab correctly.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
main_window = driver.current_window_handle
driver.get("https://www.google.com")
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.switch_to_window(main_window)
driver.get("http://www.bing.com")
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.switch_to_window(main_window)
driver.get("https://github.com/login")
There is no error while running this code.
I make a new tab after go to google.com, and the screen changes to a blank page on the new tab. After that, I wrote driver.switch_to_window(main_window) to focus on the new tab to make sure next lines of code will happen on the new tab. But as driver.get("http://www.bing.com") is executed, screen goes back to the first tab where google.com is present, and changes google.com to bing.com. Same thing for the next code. Another new tab created, but goes back to first tab as driver.get("https://github.com/login") executed and bing.com becomes github.com/login.
I thought I focused on the current visible tab by
main_window = driver.current_window_handle; driver.switch_to_window(main_window),
But this didn't work well.
How can I load all 3 webpages on 3 different tabs?
SPECIFICATIONS:
Selenium 3.0.2
firefox 45.0.1
Ubuntu 14.04 amd 64bit
python 2.7.6
Please try following solution and let me know if it's not what you're actually looking for:
driver.get("https://www.google.com")
google_window = driver.current_window_handle # Define main window
driver.execute_script("window.open('http://www.bing.com')") # Open Bing window
bing_window = [window for window in driver.window_handles if window != google_window][0] # Define Bing window
driver.execute_script("window.open('https://github.com/login')") # Open GitHub window
github_window = [window for window in driver.window_handles if window not in [google_window, bing_window]][0] # Define GitHub window
After all 3 windows opened you can simply navigate as:
driver.switch_to_window(bing_window)
...# do something
driver.switch_to_window(google_window)
...# do something
driver.switch_to_window(github_window)

Unable to invoke compose screen in Gmail/Yahoomail using selenium

Can some one help me out how can i invoke compose screen in Gmail/Yahoomail using selenium commands.
Tried with the following commands.
selenium.click("href=compose link");
selenium.click("name=Compose");
you can use webdriver and easily can invoke compose mail screen from gmail/yahoo.
See the code below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import selenium.webdriver.support.ui as ui
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re, os
import HTMLTestRunner
import xlrd
class gmail(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://gmail.com"
self.verificationErrors = []
def test_gmail_login(self):
driver=self.driver
driver.get(self.base_url +"/")
driver.find_element_by_xpath("//*[#id='Email']").clear()
print "1. enter user name in username text field"
driver.find_element_by_xpath("//*[#id='Email']").send_keys("xxxx")
driver.find_element_by_xpath(".//*[#id='Passwd']").clear()
print "2.enter password in password text field"
driver.find_element_by_xpath(".//*[#id='Passwd']").send_keys("xxxx")
print " 3. Click signIn button. it has redirect to corresponding gmail home page"
driver.find_element_by_xpath("//*[#id='signIn']").click()
print "click compose mail button"
driver.find_element_by_xpath("//*[#id=':b7']/div/div").click()
driver.save_screenshot('/compose.png')
try:
driver.find_element_by_xpath("//*[#class='z0']/div").click()
`
You can easily achieve this by using Selenium IDE. Just record the whole scenario in the SIDE and do the below steps
1. GoTo Options
2. Format
3. Click the Java/ Junit4/ Remote Control option.
Now you can see the exact Selenium RC code for the scenario you did and copy and paste it in to any IDE and make use of it.
One advisable suggestion, selenium RC is deprecated and there is no further development on RC. The future is on Selenium WebDriver. Please incorporate in to WebDriver.
Edited:
Try this code:
//Assume driver is initialized properly some where else.
driver.get("http://www.gmail.com/");
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("UserName");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("Password");
driver.findElement(By.id("signIn")).click();
//Add some wait. Use Selenium Implicit wait and Explicit wait.
Thread.sleep(5000);
driver.findElement(By.xpath("//div[2]/div/div/div/div[2]/div/div/div/div/div")).click();
driver.findElement(By.id("gbi4t")).click();
driver.findElement(By.id("gb_71")).click();
It may help you.
SIDE Screen shot:
To invoke Compose screen of Gmail in Selenium-RC using Java as below:
selenium.click("//div[text()='COMPOSE']");
To invoke Compose screen of Yahoomail in Selenium-RC using Java as below:
selenium.click("id=global_compose_top");
The following is the Selenium WebDriver Java code for gmail:
driver.findElement(By.xpath("//div[text()='COMPOSE']")).click();
The following is the Selenium WebDriver Java code for yahoo mail:
driver.findElement(By.id("global_compose_top")).click();
Use selenium wrbdriver
http://ngowda.blogspot.in/2014/01/uploading-file-in-e-commerce-site-using.html
here full code of compose a mail in gmail