Console Logging - Selenium and Katalon - selenium

I have tried two solutions for getting the INFO webdriver console logs for Chrome.
The first method,
import org.openqa.selenium.WebDriver
import org.openqa.selenium.logging.LogEntries
import org.openqa.selenium.logging.LogEntry
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser(null)
WebUI.navigateToUrl(url)
WebDriver driver = DriverFactory.getWebDriver()
LogEntries logs = driver.manage().logs().get("browser")
for (LogEntry entry : logs){
println(entry)
}
With the desired capabilities, as a list of arguments --enable-logging and --log-level=0
This method does not provide not provide any output.
The second method,
DesiredCapabilities caps = DesiredCapabilities.chrome()
LoggingPreferences logPrefs = new LoggingPreferences()
logPrefs.enable(LogType.BROWSER, Level.INFO)
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs)
WebDriver driver = new ChromeDriver(caps)
driver.get(url)
LogEntries logs = driver.manage().logs().get("browser")
This method works but forces me to launch a new instance of the webdriver and so requires me to set the desired capabilities using selenium.
I want to be able to have the desired capabilities inherited from Katalon. How can I get the first method to work?

Related

How can i use TouchActions in selenium 4

print('start')
from selenium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", True)
options.add_experimental_option('w3c', True)
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://stackoverflow.com/')
actions = TouchActions(driver)
actions.scroll(0,500).perform()
print('end')
As shown above,when running the code 'actions.scroll',it throws an exception as:
selenium.common.exceptions.WebDriverException: Message: unknown
command: Cannot call non W3C standard command while in W3C mode
I don't want to modify the configuration 'w3c' as 'False',is there any other way to use TouchActions in selenium 4? The operating environment is as follows:
selenium 4.1.0
ChromeDriver 97.0.4692.71
Google Chrome 97.0.4692.71
TouchActions has been deprecated as per v8 Appium - https://github.com/appium/java-client/blob/master/docs/v7-to-v8-migration-guide.md#touch-actions
The below code is an example of longpress based on w3Actions
WebElement tc=driver.findElement(By.xpath("Your Xpath"));
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence longpress = new Sequence(finger, 1);
longpress.addAction(finger.createPointerMove(Duration.ofMillis(0),
PointerInput.Origin.viewport(), tc.getLocation().x,tc.getLocation().y));
longpress.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg());
longpress.addAction(new Pause(finger,Duration.ofSeconds(3)));longpress.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(longpress));

How to disable navigator.webdriver using Selenium in VBA? [duplicate]

Been searching for a while and tried all the solutions present but none appear to be working. I created a "slide show" that will first log in, then alternate between tabs. All of that is working but i cannot get rid of the
"Chrome is being controlled by automated test software" bar. Any advise?
Code
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
usernameStr = 'test'
passwordStr = 'test'
browser = webdriver.Chrome()
#first tab
browser.get(('www.testwebsite.com?'))
# fill in username and hit the next button
username = browser.find_element_by_id('username')
username.send_keys(usernameStr)
password = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, 'password')))
password.send_keys(passwordStr)
nextButton = browser.find_element_by_class_name('emp-submit')
nextButton.click()
#second tab
browser.execute_script("window.open('about:blank', 'tab2');")
browser.switch_to.window("tab2")
browser.get('www.testwebsite.com')
Try this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches",["enable-automation"])
driver_path = '/Users/myuser/Downloads/chromedriver'
driver = webdriver.Chrome(executable_path=driver_path, chrome_options=options)
driver.get('https://google.com')
driver.close()
When you open Chrome Browser in through ChromeDriver this infobar containing the notification is embedded as follows:
Chrome is being controlled by automated test software
Browser snapshot without the argument disable-infobars:
But if you add the argument disable-infobars through an instance of ChromeOptions you can get rid of this infobar as follows:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.google.com/')
Browser snapshot applying the argument disable-infobars:
THIS IS WORKING WITH LATEST RELEASE.
Try it by changing driver path under "service_obj"
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])
service_obj = Service(r"C:\Users\Documents\Sublime_srk\drivers\chromedriver_win32\chromedriver.exe")
driver = webdriver.Chrome(options=chrome_options,service=service_obj)
driver.get("https://www.google.co.in/")
Click on the "x" to close the bar. It doesn't work initially, but then maximize and restore the window and it should disappear. This is for anyone who doesn't trigger their tests through Java.

How to open Chrome browser console through Selenium?

I want to open chrome browser console by pressing keyboard keys Ctrl+Shift+j in selenium webdriver.
I am able to do this action using Robot class but I want this without Robot class. I have used the Actions class and Keys class using sendKeys. But I am unable to open browser console.
Is it chrome browser version issue or OS? Why the browser console is not opening using Action class and Keys class. ?
To open chrome browser console you can use the ChromeOptions class with --auto-open-devtools-for-tabs argument as follows:
Test Configuration:
Selenium: Selenium Standalone Server v3.14.0
ChromeDriver: ChromeDriver 2.46.628402
Chrome: Google Chrome 72.0.3626.96
Code Block:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class A_Chrome_Browser_Console {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
options.addArguments("--auto-open-devtools-for-tabs");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
}
}
Console Output:
Google
Browser Console Snapshot:
You can find a relevant python based discussion in Opening inspect (pressing F12) on Chrome via Selenium

Page load strategy for Chrome driver (Updated till Selenium v3.12.0)

I'm using Chrome browser for testing WebApp.
Sometimes pages loaded after very long time. I needed to stop downloading or limit their download time.
In FireFox I know about PAGE_LOAD_STRATEGY = "eager".
Is there something similar for chrome?
P.S.: driver.manage().timeouts().pageLoadTimeout() works, but after that any treatment to Webdriver throws TimeOutException.
I need to get the current url of the page after stopping its boot.
ChromeDriver 77.0 (which supports Chrome version 77) now supports eager as pageLoadStrategy.
Resolved issue 1902: Support eager page load strategy [Pri-2]
From the Webdriver specs:
For commands that cause a new document to load, the point at which the command returns is determined by the session’s page loading strategy.
When Page Loading takes too much time and you need to stop downloading additional subresources (images, css, js etc) you can change the pageLoadStrategy through the webdriver.
As of this writing, pageLoadStrategy supports the following values :
normal
This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).
eager
This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).
none
This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).
By default, when Selenium loads a page, it follows the normal pageLoadStrategy.
Here is the code block to configure pageLoadStrategy() through both an instance of DesiredCapabilities Class and ChromeOptions Class as follows : :
Using DesiredCapabilities Class :
package demo; //replace by your own package name
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class A_Chrome_DCap_Options {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
DesiredCapabilities dcap = new DesiredCapabilities();
dcap.setCapability("pageLoadStrategy", "normal");
ChromeOptions opt = new ChromeOptions();
opt.merge(dcap);
WebDriver driver = new ChromeDriver(opt);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}
Using ChromeOptions Class :
package demo; //replace by your own package name
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class A_Chrome_Options_test {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions opt = new ChromeOptions();
opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(opt);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}
Note : pageLoadStrategy values normal, eager and none is a requirement as per WebDriver W3C Editor's Draft but pageLoadStrategy value as eager is still a WIP (Work In Progress) within ChromeDriver implementation. You can find a detailed discussion in “Eager” Page Load Strategy workaround for Chromedriver Selenium in Python
References:
WebDriver navigation
WebDriver page load strategies
WhatWG Document readyStateChange / readiness
For Selenium 4 and Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.page_load_strategy = 'none'
driver = webdriver.Chrome(options=options)
driver.get("http://www.google.com")
driver.quit()
For more details can be found here https://www.selenium.dev/documentation/webdriver/capabilities/shared/#none
In C#, since PageLoadStrategy.Eager doesn't seem to work for Chrome, I just wrote it myself with a WebDriverWait. Set the PageLoadStrategy to none and then doing this will basically override it:
new WebDriverWait(_driver, TimeSpan.FromSeconds(20))
.Until(d =>
{
var result = ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState");
return result.Equals("interactive") || result.Equals("complete");
});
You just add in your chrome driver as a parameter and the TimeSpan is set to a max of 20 seconds in my case. So it will wait a max of 20 seconds for the page to be interactive or complete
Try using explicit wait . Visit this link. It might be helpful
Try this code as well:
WebDriver driver = new FirefoxDriver();
String startURL = //a starting url;
String currentURL = null;
WebDriverWait wait = new WebDriverWait(driver, 10);
foo(driver,startURL);
/* go to next page */
if(driver.findElement(By.xpath("//*[#id='someID']")).isDisplayed()){
String previousURL = driver.getCurrentUrl();
driver.findElement(By.xpath("//*[#id='someID']")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
ExpectedCondition e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return (d.getCurrentUrl() != previousURL);
}
};
wait.until(e);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
}
I hope your problem will be resolved using above code

splinter: how to add chrome options?

I'm using splinter(v0.7.3) for web testing under linux, while on chrome, the default sample code can not running:
from splinter import Browser
from pyvirtualdisplay import Display
d = Display(visible=0, size=(800, 600))
d.start()
b = Browser('chrome')
b.visit('http://www.google.com')
b.quit()
d.stop()
While running, I got the exception like this:
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
And I test the same function in selenium with some chrome option added:
from selenium import web driver
from selenium.webdriver.chrome.options import Options
from pyvirtualdisplay import Display
d = Display(visible=0, size=(800, 600))
d.start()
opt = Options()
opt.add_argument('--disable-setuid-sandbox')
b = webdriver.Chrome(chrome_options=opt)
b.get('http://www.google.com')
b.quit()
d.stop()
This works ok, the difference is the --disable-setuid-sandbox option added to chrome driver, if the option not add, there would be a zombie chrome-sandbox process under chromium-browser.
The problem here is, I don't know how to pass a chrome.options.Option instance to splinter.Browser(), I browsed the implementation under splinter/driver/webdriver/chrome.py, it seems that there is no entry to pass such a instance to splinter.Browser(). Is there some other way to pass options to chrome driver?
Create a new instance of BaseWebDriver and set .driver with an instance of the Chrome driver. This example starts Chrome maximized:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from splinter.driver.webdriver import BaseWebDriver, WebDriverElement
options = Options()
options.add_argument('--start-maximized')
browser = BaseWebDriver()
browser.driver = Chrome(chrome_options=options)
browser.visit('https://www.google.com')
The only way I could ever do this was by using the add_argument method with selenium.webdriver.ChromeOptions like so:
from selenium.webdriver import ChromeOptions
from splinter import Browser
chrome_options = ChromeOptions()
chrome_options.add_argument(your_argument)
b=Browser("chrome", options=chrome_options)
b.visit('http://www.google.com')
b.quit()
so in your code would be:
from splinter import Browser
from selenium.webdriver import ChromeOptions
from pyvirtualdisplay import Display #I'm not certain what this is...
d = Display(visible=0, size=(800, 600))
d.start()
chrome_options = ChromeOptions()
chrome_options.add_argument('disable-setuid-sandbox')
b = Browser('chrome')
b.visit('http://www.google.com')
b.quit()
d.stop()
Note: I was unable to test this with your argument specifically because I recently broke my GRUB so I am stuck in windows, and the disable-setuid-sandbox option is linux-only. However, I have been using this method with the headless argument for a while.
I am not 100% sure that this will work but I just looked at the docs for splinter and it says.
You can also pass additional arguments that correspond to Selenium DesiredCapabilities arguments.
Looking into the sourcecode of Splinter calling Browser can take some arguments. These arguments will then be passed to create an Instance of the Chrome WebDriver. So I went to the selenium sourcecode and saw the constructor looks like this:
def __init__(self, executable_path="chromedriver", port=0,
chrome_options=None, service_args=None,
desired_capabilities=None, service_log_path=None):
There is a parameter for chrome_options so it should be possible to pass it using this parameter. So if I'm correct this should work fine:
opt = Options()
opt.add_argument('--disable-setuid-sandbox')
b = Browser(browser='chrome', chrome_options=opt)
Edit
Alternatively you could pass the options as desired capabilities aswell:
opt = Options()
opt.add_argument('--disable-setuid-sandbox')
dc = opt.to_capabilities()
b = Browser(browser='chrome', desired_capabilities=dc)
I've been working on a fork of splinter for the past couple weeks, you can check out my dev branch if you want. I have added this and other features.
Options can be passed as a list to the chrome_options parameter
from splinter import Browser
options = ['--start-maximized', '--disable-setuid-sandbox']
with Browser('chrome', chrome_options=options) as browser:
browser.visit('http://www.google.com')
Edit:
So it turns out this was possible with splinter all along just using **kwargs which passes the various available options to the selenium driver(s). For example:
from splinter import Browser
options = {'chrome_options':['--start-maximized', '--disable-setuid-sandbox']}
with Browser('chrome', **options) as browser:
browser.visit('http://www.google.com')