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.
Related
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()
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.
I have written a (Python) script using Selenium to automate some downloads from Box (I'm aware there is a Box API, but I'm accessing school resources and I'm not sure that the API would interact well with the school's external authentication). The script works using the Chrome driver in non-headless mode, but not in headless.
The step where I'm having issues is an attempt to query the status of downloads. I think my script is actually able to execute the downloads correctly: I've added some code from SO to disable security defaults that prevent downloading in headless mode.
I've successfully queried the download status using two methods (both executing Javascript in Chrome):
Selenium (Python) - waiting for a download process to complete using Chrome web driver
https://gist.github.com/ic0n/a38b354cac213e5aa50c55a0d8b87a0b
Both stop working when I switch to headless mode.
Here's the actual code I'm using:
# Headless Chrome disables downloads by default (at least as of 2017).
# https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium
def enable_download_in_headless_chrome(driver, download_dir):
# add missing support for chrome "send_command" to selenium webdriver
driver.command_executor._commands["send_command"] = \
("POST", '/session/$sessionId/chromium/send_command')
params = {
'cmd': 'Page.setDownloadBehavior',
'params': {'behavior': 'allow', 'downloadPath': download_dir},
}
return driver.execute("send_command", params)
def get_top_download_state(driver):
"""Call this after running driver.get("chrome://downloads")."""
[state, percent, progress] = driver.execute_script("""
var item = downloads.Manager.get().items_[0];
var state = item.state;
var percent = item.percent;
var progress = item.progressStatusText;
return [state, percent, progress];
""")
return state, percent, progress
Errors for the first method say that downloads is not defined. Errors for the second method say that you can't get the shadowRoot property of null (presumably the query selectors are just finding nothing).
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)
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