I am using nCino application (build on Salesforce) for my project. nCino is a framework built on Salesforce application and called in a .
I am trying to automate functional flows in nCino UI through Selenium Java in Chrome browser (tried in Firefox too)
Issue 1: Switch to iframe and click on element
Refer to iframe code below
Console error:
Selenium Code Snippet to switch to iframe and click element inside element:
package nCInoAutomation;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.http.Header;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.html5.ApplicationCache;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Main {
public static void main(String args[]) throws InterruptedException {
//System.setProperty("webdriver.gecko.driver", Parameters.FIREDRIVER);
System.setProperty("webdriver.chrome.driver", Parameters.CHROMEDRIVER);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
// chromeOptions.addArguments("--disable-web-security");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//WebDriver driver = new FirefoxDriver();
driver.get(Parameters.APPURL);
Thread.sleep(2000);
WebElement formElement = driver.findElement(By.id("login_form"));
formElement.findElement(By.id(Parameters.UNAME_XPATH)).sendKeys(Parameters.UNAME);
formElement.findElement(By.id(Parameters.PWD_XPATH)).sendKeys(Parameters.PWD);
formElement.findElement(By.id(Parameters.ENTER_XPATH)).sendKeys(Keys.ENTER);
Thread.sleep(20000);
System.out.println("Wait Over");
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(2));
List<WebElement> list = driver.findElements(By.tagName("iframe"));
System.out.println(list.size());
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement ele = list.get(2);
js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", ele);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,500)", "");
System.out.println("********Switched to the iframe*******");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#id='ncSecondaryNavigation']//span[text()='Loan'])[2]")));
System.out.println("Clicked");
}
}
It says that it has switched to iframe but doesnt look like so.
Issue 2: Unable to locate element
Also, iframe contains most of the angular js elements hence Unable to identify elements. See screenshot below:
Application looks somewhat like below:
Can you please help me automate below:
1. Switch to iframe once landed on Salesforcw screen.
2. Navigate to 'Collateral' screen by clicking on the link.
https://www.phptravels.net has two input boxes with same name and same attributes (Enter City Or Airport).Consequently, my xpath results in "no such element" exception. Is there any work around. Please help! Following are the code:
package com.php.travels;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Booking {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/Users/owner/desktop/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.phptravels.net");
driver.findElement(By.xpath("//span[text()='Flights']")).click();
driver.findElement(By.xpath("//*[#id='select2-
drop']/div/input")).sendKeys("LAX");
}
}
The problem is not about same ID for both elements.
The id will be changed when you click on the text field. It means you will find only single input element each time. You don't need to worry if they have the same ID or not.
You have to click at the text field before finding the input element. See my code below.
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.phptravels.net");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Flights']")));
element.click();
driver.findElement(By.xpath("//*[#id='s2id_location_from']")).click();
driver.findElement(By.xpath("//*[#id='select2-drop']/div/input")).sendKeys("LAX");
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='select2-results']//li[contains(.,'LAX')]")));
element.click();
driver.findElement(By.xpath("//*[#id='s2id_location_to']")).click();
driver.findElement(By.xpath("//*[#id='select2-drop']/div/input")).sendKeys("DMK");
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='select2-results']//li[contains(.,'DMK')]")));
element.click();
Both the input boxes have different names
<input type="text" name="" id="location_from" tabindex="-1" class="select2-offscreen">
<input type="text" name="" id="location_to" tabindex="-1" class="select2-offscreen">
So you can have the following css selectors for the first and second input boxes
For first input box : input#location_from.select2-offscreen
For second input box : input#location_to.select2-offscreen
I have tried both these locators and they work perfectly fine.
Problem is with the locators only if you can get the exact locator then there would not be the problem:
package com.php.travels;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Booking {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/Users/owner/desktop/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.get("https://www.phptravels.net");
driver.findElement(By.xpath("//span[#class='hidden-xs' and contains(text(),'Flights')]")).click();
driver.findElement(By.xpath("//span[#class='select2-chosen' and contains(text(),'Enter City Or Airport')]")).click();
driver.findElement(By.xpath("//div[#class='select2-drop-mask']/following-sibling::div//input[contains(#class,'select2-input')]")).sendKeys("LAX");
}
}
I have few fields along with text field ,date, rich text editor, dropdown and browse button to upload image. I am getting error as unable to locate element on selecting the dropdown after rich text editor. Here is my code:
package newpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class MyClass {
public static void main(String args[])throws NoSuchElementException,InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("url");
driver.findElement(By.id("area_number")).sendKeys("1221");
driver.findElement(By.id("street_name")).sendKeys("abc");
driver.findElement(By.id("email_of_owner")).sendKeys("test#gmail.com");
driver.findElement(By.id("name_of_contact")).sendKeys("Test Papri");
driver.findElement(By.id("contact_date")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/button[4]")).click();
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.tagName("iframe"))));
//Code for Rich text editor goes here//
jsExecutor.executeScript("window.scrollTo(0, document.body.scrollHeight)");
//Active value is being displayed in a li tag as selected after a button is clicked so tried with this.
WebDriverWait wait1 = new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.elementToBeSelected(By.xpath("/html/body/section[2]/div/div[2]/div/div/div/div[2]/div[1]/form/div[7]/div[2]/div/div/div/div/ul/li[2]/a/span[1]")));
//Select dropdown is a button so tried to click it.This is the xpath of the button
WebElement dropdown = driver.findElement(By.xpath("/html/body/section[2]/div/div[2]/div/div/div/div[2]/div[1]/form/div[7]/div[2]/div/div/div/button"));
//dropdown.click();
//driver.close();
}
}
If any one logged inside any application using chrome browser,notification pop-up appears to save password/Allow notification.
How to handle this notification pop-up through selenium web-driver? Sometimes two pop-ups appears(one is for save password and another one is to allow notification).I have already tried to handle using Alert class but could not succeeded.kindly help me on this.
You can open use ChromeOptions Class. Below is the sample code.
ChromeOptions chrome_Profile = new ChromeOptions();
chrome_Profile.addArguments("chrome.switches","--disable-extensions");
chrome_Profile.addArguments("--disable-save-password");
chrome_Profile.addArguments("disable-infobars");
System.setProperty("webdriver.chrome.driver","c/chromedriver.exe");
//Passing chrome_Profile while initializing the ChromeDriver
WebDriver driver = new ChromeDriver(chrome_Profile);
Let me know if this helps.
You can use below code to allow chrome to send notifications:
ChromeOptions options=new ChromeOptions();
Map<String, Object> prefs=new HashMap<String,Object>();
prefs.put("profile.default_content_setting_values.notifications", 1);
//1-Allow, 2-Block, 0-default
options.setExperimentalOption("prefs",prefs);
ChromeDriver driver=new ChromeDriver(options);
#Pritesh patel
it didnt work for me.. I want to allow the flash to run
package com.selenium.Basics;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import Flash.FlashObjectWebDriver;
public class GuruPgm14FlashTesting {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String driverPath="C:\\selenium\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions options=new ChromeOptions();
Map<String, Object> prefs=new HashMap<String,Object>();
prefs.put("profile.default_content_setting_values.notifications", 1);
//1-Allow, 2-Block, 0-default
options.setExperimentalOption("prefs",prefs);
WebDriver driver=new ChromeDriver(options);
driver.get("http://demo.guru99.com/test/flash-testing.html");
Thread.sleep(5000);
driver.findElement(By.xpath("//embed[#play='false']")).click();
System.out.println("Clicked to allow the addon");
Thread.sleep(5000);
FlashObjectWebDriver fdriver= new FlashObjectWebDriver(driver, "myFlashVideo");
fdriver.callFlashObject("Play");
Thread.sleep(5000);
fdriver.callFlashObject("StopPlay");
Thread.sleep(5000);
fdriver.callFlashObject("SetVariable","/:message","Flash testing using selenium Webdriver");
System.out.println(fdriver.callFlashObject("GetVariable","/:message"));
}
}
const webdriver = require('selenium-webdriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
var chromeOptions = {
'args': ['--disable-notifications']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
If you are using js. Especially when website adding now option to provide add on your screen or even the www dot come want to send you notification, this ultimately makes your windows blackened and inaccessible to element in it.
Try to put this on a different JS file if u do. Cause export is a function
in Python:
if browser == "chrome":
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.driver.maximize_window()
self.driver.implicitly_wait(10)
You can use ChromeOptions class to allow / disable notification popup.
Please find code below :
Map<String, Object> prefs = new HashMap<String, Object>();
// 1 for allowing, 2 for disabling popup
prefs.put("profile.default_content_setting_values.notifications", 1);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
We can disable all type chrome notifications such as save password, allow locations..., just by adding only one argument.
ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", ""c/chromedriver.exe"");
WebDriver driver = new ChromeDriver(ops);
hope this helps you.
Thanks.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
public class XpathDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new SafariDriver();
driver.get("http://www.amazon.in/");
//driver.findElement(By.xpath("b[style='padding-right:3px; font-weight:normal;font-size: 18px;text-transform: uppercase;']")).click();
driver.findElement(By.cssSelector("#Sign in")).click();
//driver.findElement(By.cssSelector("[class = '.nav-action-inner']")).click();
//driver.findElement(By.cssSelector("#ap_email")).sendKeys("vit.yoamitsharma#gmail.com");
//driver.findElement(By.xpath("html/body/div[1]/header/div/div[1]/div[4]/div[7]/div[2]/a/span")).click();
driver.findElement(By.linkText("Sign in")).click();
}
}
Whats the error here?
I see 2 un-commented lines trying to click sign in button.
Have you tried with xpath checker? following should work and it worked for me
driver.findElement(By.xpath("html/body/div[1]/header/div/div[1]/div[4]/div[7]/div[2]/a/span")).click();