If you go to https://www.google.com, go into your browser's Developer Tools, and click on "Storage -> Session Storage -> https://www.google.com", you'll see three key:value pairs there:
_c;;i:"p:*|l:9007199254740991_2"
hsb;;1635503738313:"p:*|l:1_{"state":null,"url":"/","metadata":{"Nua":1635503738312,"Mj":1635503738313,"f6":1635503738314,"hz":0}}"
hsb;;1635503738314:"p:*|l:0_[1635503738313]"
How can you return those Session Storage values with a Selenium script?
driver.execute_script("return sessionStorage.length") returns 0, indicating that the values are not there.
I had to read the session storage key value pairs using Selenium.
I tried the following commands for the Google webpage session variables and it worked perfectly fine
I did it on C# though
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string key = (string)js.ExecuteScript(“return sessionStorage.key(0)”);
string value = (string)js.ExecuteScript(“return sessionStorage.getItem(‘“ + key + “‘)”);
The key and value will have the values of the session variables that can be seen on the developer tools
Related
I manually open up the Chrome browser and go to 'https://www.google.com/' and hit F12 to go to developer tools. I then go to Console and type in localStorage.key(0) to get the key of the first item in local storage and I get 'whatever'.
However, when I run the following test code the returned value in firstKey is Empty.
Sub GetLocalStorage()
Dim driverGoogle As Object
Dim firstKey As Variant
Set driverGoogle = CreateObject("Selenium.Chromedriver")
driverGoogle.Get "https://www.google.com/"
driverGoogle.Window.SetSize 1800, 1050
firstKey = driverGoogle.ExecuteScript("localStorage.key(0);")
End Sub
I even changed localStorage.key(0) to incorrect localStorage.aaakey(0) make sure ExecuteScript was actually executing and sure enough Selenium threw an error.
So, how to get Local Storage key/values?
You have to add delay to let the site loaded.
Currently you are trying to apply driverGoogle.ExecuteScript("localStorage.key(0);") script immediately after sending the URL to the web driver while the page just starting loading.
The ExecuteScript string needed the 'return' instruction. Once added everything works fine.
firstKey = driverGoogle.ExecuteScript("return localStorage.key(0);")
I want to test how my app reacts to numpad keys. I found in https://www.w3.org/TR/webdriver/ specs that for example for Numpad Home (with location = DOM_KEY_LOCATION_NUMPAD = 3) a symbol \uE057 should be used. However, it doesn't work for me: I get Home with default location (0), moreover, event.code is empty. It gives me a different result when I physically press NUMPAD7 button with Num Lock off: it that case, I get correct location 3 and event.code is Numpad7.
var options = FirefoxOptions();
options.setLogLevel(FirefoxDriverLogLevel.TRACE);
var driver = FirefoxDriver(options);
driver.navigate().to("https://keycode.info/");
driver.findElementByTagName("body").sendKeys("\uE057");
So how can I send such a key? I'm now thinking of manual recording of generated events when I physically press a key, and then sending these events via Selenium's execution of JS script. However, I haven't tried it yet; maybe there is a better way to do it in Selenium; maybe there is another framework that allows it better.
By the way, I've filed a similar ticket in geckodriver because it looks like a bug of webdriver to me...
\ue01d is the unicode for NUmberpad3
python code:
from selenium import webdriver
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome()
driver.get("https://keycode.info/")
driver.find_element_by_tag_name("body").send_keys("\ue01d")
input()
you can use https://www.javadoc.io/doc/org.seleniumhq.selenium/selenium-api/latest/org/openqa/selenium/Keys.html to send keys instead of sending unicode directly
In firefox this is will work if you use action chain:
ActionChains.send_keys("\ue01d").perform()
i am writing a tool to check mac address online with selenium i managed to find the input and the submit but when i ask for the results it print the session id and the token
import selenium
## set up options
options = Options()
options.headless=True
browser.Firefox(options, exceutable_path=r"geckodriver_path")
browser.get("site-URL")
## mac address sent to site
elem = browser.find_element_by_id('result')
elemnt = browser.find_element_by_css_selector('#results-log')
print (elem)
print (elemnt)
the output is some session info
<selenium.webdriver.remote.webelement.WebElement (session="289e304328d8a7900f7003d4ed6530be", element="f807a2e7-8895-4e8d-b7af-ce3d27fbf897")>
i need to get the result that is on the site
You saw it right.
The variable elem is a WebElement identified through browser.find_element_by_id('result')
The variable elemnt is a WebElement identified through browser.find_element_by_css_selector('#results-log')
Printing the element will be in the following format:
<selenium.webdriver.remote.webelement.WebElement (session="289e304328d8a7900f7003d4ed6530be", element="f807a2e7-8895-4e8d-b7af-ce3d27fbf897")>
You can find a relevant discussion in Are element IDs numbers in Webdrivers?
In Firefox browser developer tool console, I type var a = libraryform.firstname
It returns the firstname value entered by user for that form.
I am new to protractor and selenium. How can I call library.firstname in protractor to get the value of fistname?
Use Javascript Executor to execute the javascript which you able to get them working in developer console against your application.
For e.g.,
this returns web-element which retrieved through javascript executed in browser.
var element = browser.executeScript("document.getElementById('identifier1')");
Refer this page for more examples
If this single line returns required value in browser console, then the protractor code should be some thing like this,
var name = browser.executeScript('return libraryform.firstname')
If you need too many lines of js, you can specify with semicolon separated,
var name = browser.executeScript("var element=$('.dropdown-toggle').eq(0); return element.text();")
I am new-ish to Selenium, so I use Katalon Automation Recorder through Chrome to quickly draft scripts.
I have a script that makes an account on a website, but I want to make more than one account at a time (using a catchall). Is there a way for Selenium/Katalon to alternate its input from a database of preset emails (CSV sort of thing) or even generate random values in-front of the #domain.com each time the script loops over?
Here is the current state of the script:
Thanks
As #Shivan Mishra mentioned, you have to do some data driven testing. In Katalon you can created test data in object repository (See https://docs.katalon.com/katalon-studio/docs/manage-test-data.html)
You can manage your test data in script like following example:
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
def data = findTestData('path/to/your/testdata/in/object repository')
for(int=0;i<data.getRowNumbers();i++){
def value = data.getValue(1, i)
// do any action with your value
}