Unable to sendkeys using webdriverjs specifically F11 to maximise the browser - javascript

With the below code block it opens a chrome browser fine it just won't full screen the browser using F11. i used to use C# and selenium and that worked fine using this method on chrome and different browsers. It finds the element 'body' but then does not send the key press. Am I doing something wrong here that i should be requiring some other library?
the documentation for webdriverjs is pathetic and there is very few examples, I am seriously considering dumping it for something else possibly python.
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('https://www.google.co.uk/');
driver.wait(function () {
return driver.getTitle().then(function (title) {
return title === 'Google';
});
}, 1000);
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys("F11");
why are we doing this. we are developing a website that will change depending on size 800x600 + with and without the toolbar depending on how the screen is used different items will be displayed. i can maximise the window using,
driver.manage().window().maximize();
This however still leaves the toolbar present and doesn't act as if the user has pressed the F11 key.

it tooks some time to find it but you should have all the Keys in webdriver.Key
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys(webdriver.Key.F11);
Hope it helps!

A co-worker has just discovered that it works well in C# with:
Driver.Instance.Manage().Window.FullScreen();

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).send_keys(Keys.F11).perform()
I use a similar command to toggle JS via Firefox's NoScript add-on
edit: I just tested, and it works!

This should work for you:
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys(Keys.F11);

You can maximise the window using:
driver.manage().window().maximize();

For me the one emulating correctly the F11 on startup is:
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-fullscreen");
WebDriver driver = new ChromeDriver(options);
Or if the kiosk mode is preferred:
ChromeOptions options = new ChromeOptions();
options.addArguments("--kiosk");
WebDriver driver = new ChromeDriver(options);

Related

Is the Firefox Web Console accessible in headless mode?

The title says it all: I am wondering whether it is possible to interact with the Firefox console upon starting Firefox in headless mode.
More generally, I'd settle for some way of accessing it programmatically, in scripts.
What I've tried:
So far I've been playing with the Javascript bindings to Selenium without success:
Starting Firefox with the -devtools option from Selenium does opn the dev tools, but I then cannot send key combinations that will switch me to the actual console, or in fact interact from my .js script with the open devtools window in any way.
Edit
In response to the first comment below: this answer does not seem to help. The console is not opened when I send CTRL+SHIFT+k to the body tag of google.com:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var firefox = require('selenium-webdriver/firefox');
var inpt = require('selenium-webdriver/lib/input');
var options = new firefox.Options();
var driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
(async function(){
await driver.get('https://google.com');
var bdy = await driver.findElement(By.id('gsr'));
await bdy.sendKeys(inpt.Key.CONTROL + inpt.Key.SHIFT + 'k');
})();
This opens the page (google.com) and returns no errors, but there's no console anywhere.
For good measure: sending just inpt.Key.SHIFT + 'k' does enter a capital 'K' in the Google search field, so I know the keys are referenced correctly.
Also, sending just 'k' enters a small 'k' in the search field. It's only the three-key combo that does not work.
2nd edit:
I take it back: the newer answer does work, precisely as-is (I switched to Python from node).
The comment below by Karthik does resolve the matter, but I would like to summarize here and document working solutions that automate Firefox-Web-Console access.
The point of the answer I linked to above (in my 2nd edit) is that in order to have full access to the Firefox browser key controls one must
first switch Firefox context to chrome (from the default content context)
direct the automated browser driver to locate the element carrying id tabbrowser-tabs
send the key combo (in this case Ctrl+Shift+k) to that element.
Concrete working solutions:
Python
The script is
from selenium.webdriver import Firefox, DesiredCapabilities, FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import time
options = Options()
webdriver = Firefox(options=options)
webdriver.get("https://google.com")
try:
time.sleep(3)
with webdriver.context(webdriver.CONTEXT_CHROME):
console = webdriver.find_element(By.ID, "tabbrowser-tabs")
console.send_keys(Keys.LEFT_CONTROL + Keys.LEFT_SHIFT + 'k')
except:
pass
Run with python <path-to-script> it opens a Firefox window displaying google.com and the console at the bottom.
Javascript
Here the full script is
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var firefox = require('selenium-webdriver/firefox');
var inpt = require('selenium-webdriver/lib/input');
var options = new firefox.Options();
var driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
(async function(){
await driver.get('https://google.com');
await driver.setContext("chrome");
var tabs = await driver.findElement(By.id('tabbrowser-tabs'));
await tabs.sendKeys(inpt.Key.CONTROL + inpt.Key.SHIFT + 'k');
})();
Run with node <path-to-script> it achieves the same effect as above: a Firefox window open on google.com, with the console open at the bottom.

how to make safari browser in full screen while running automation test

I am using java + selenium webdriver for webautomaiton.
For safari browser 10.1 version, I need the browser to be full screen before test started.However
driver.manage().window().maximize();
does not work
I tried few options but no luck.
1.
seems no option available for doing something like which would write in the plist file of /Library/Preferences folder of mac
defaults write com.apple.Safari
2
WebElement element = Wait.wait.until(visibilityOfElementLocated(By.cssSelector(".logo-large")));
element.sendKeys(Keys.CONTROL , Keys.COMMAND , "f");
element.sendKeys(Keys.CONTROL , Keys.COMMAND , "F");
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.COMMAND).sendKeys("F").perform();
action.keyDown(Keys.CONTROL).keyDown(Keys.COMMAND).sendKeys("f").perform();
Is there anyway I can do it using send keys, or write in the plist file or through javascript.
Try below :-
public static void maximizeScreen(WebDriver driver) {
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point position = new Point(0, 0);
driver.manage().window().setPosition(position);
Dimension maximizedScreenSize =
new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(maximizedScreenSize);
}
Hore it will help you
Take a look here (Native Fullscreen JavaScript API): http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/
Sample:http://johndyer.name/lab/fullscreenapi/
You have to update the safari browser, update the browser with Safari 11+ version
then you are able to maximize it using this code:
driver.manage().window().maximize();

javascript + Selenium WebDriver cannot load list of followers in instgram

I am learning JavaScript,node.js and Selenium Web Driver.
As part of my education process I am developing simple bot for Instagram.
To emulate browser I use Chrome web driver.
Faced problem when trying to get list of followers and amount of followers for the account:
This code opens instagram page, enters credentials, goes to some account and opens followers for this account.
Data like username and password I take from the settings.json.
var webdriver = require('selenium-webdriver'),
by = webdriver.By,
Promise = require('promise'),
settings = require('./settings.json');
var browser = new webdriver
.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
browser.manage().window().setSize(1024, 700);
browser.get('https://www.instagram.com/accounts/login/');
browser.sleep(settings.sleep_delay);
browser.findElement(by.name('username')).sendKeys(settings.instagram_account_username);
browser.findElement(by.name('password')).sendKeys(settings.instagram_account_password);
browser.findElement(by.xpath('//button')).click();
browser.sleep(settings.sleep_delay);
browser.get('https://www.instagram.com/SomeAccountHere/');
browser.sleep(settings.sleep_delay);
browser.findElement(by.partialLinkText('followers')).click();
This part should open all followers, but not working:
var FollowersAll = browser.findElement(by.className('_4zhc5 notranslate _j7lfh'));
Tried also by xpath:
var FollowersAll = browser.findElement(by.xpath('/html/body/div[2]/div/div[2]/div/div[2]/ul/li[3]/div/div[1]/div/div[1]/a'));
When I run in the browser's console:
var i = document.getElementsByClassName('_4zhc5 notranslate _j7lfh');
it is working fine.
I run code in debug mode (use WebStorm) and it shows in each case that variable "FollowersAll" is undfined.
The same happens when I try to check amount of followers for the account.
Thanks in advance.
example of the selected element
In DOM, class names may be used multiple time. In this case, findElement by className wont work.
Xpath should be Relative and should not be Absolute.
Try Xpath with unique HTML Attribute. For example:
1. //div[#id/text()='value']
In chrome browser, open Developer Tools(press F12). If you framed an Xpath, just press Ctrl+F and paste that Xpath. If it states 1 of 1, then you can surely use that Xpath.
If it states 1 of many, then you need to dig deeper to take exact Xpath.

Setting specific capabilities for selenium webdriver

I'm trying to set some specific capabilities for a Selenium Webdriver.
In this example i want to change a capability of the webdriver for the Firefox browser. I'm trying to this in Javascript.
this.driver = new selenium.Builder().forBrowser('firefox').build();
I tried things like calling .withCapabilities() but it is not working as i expected it and crashes the program.
In specific i want to set the 'acceptSslCerts' capability to true because it is false in default.
Does anybody have an idea on how to this?
I'm not able to find anything in the API reference or on the internet.
This works fine for me, to set CORS in my chrome browser.
const { Builder, By, Key, until } = require('selenium-webdriver');
const capabilities = {
'chromeOptions': {
'args': ['--disable-web-security', '--user-date-dir']
}
}
let driver = new Builder().withCapabilities(capabilities).forBrowser('chrome').build();
For firefox, through FirefoxProfile, you can accept SSL certificate.
e.g FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true)
For googlechrome, through DesiredCapability, you can set the browser property.

JMeter - WebDriver Sampler - waitForPopUp

I am trying to work out a comparable command to use in jmeter webdriver sampler (JavaScript) how to do a waitForPopUp command. There must be a way. I have something that works for waiting for an element, but I can't work it out for a popup.
Update
I am using this code for waiting for an element:
var wait = new support_ui.WebDriverWait(WDS.browser, 5000)
WaitForLogo = function() {
var logo = WDS.browser.findElement(org.openqa.selenium.By.xpath("//img[#src='/images/power/ndpowered.gif']"))
}
wait.until(new com.google.common.base.Function(WaitForLogo))
And this works, but I can't work out how reuse this to wait for a popup, that has no name, in Java I have used:
selenium.waitForPopUp("_blank", "30000");
selenium.selectWindow("_blank");
And that works, but I can't work out an comparable JavaScript that will work in Jmeter for performance, as I can't get Java working in Jmeter.
I was able to get this working using:
var sui = JavaImporter(org.openqa.selenium.support.ui)
and:
wait.until(sui.ExpectedConditions.numberOfWindowsToBe(2))
In WebDriver Sampler you have the following methods:
WDS.browser.switchTo.frame('frame name or handle') - for switching to a frame
WDS.browser.switchTo.window('window name or handle') - for switching to a window
WDS.browser.switchTo.alert() - for switching to a modal dialog
WDS.browser.getWindowHandles() - for getting all open browser window handles
See JavaDoc on WebDriver.switchTo method and The WebDriver Sampler: Your Top 10 Questions Answered guide for more details.

Categories

Resources