Unable to locate a dropdown in selenium - selenium-webdriver

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();
}
}

Related

How to make a click on button with selenium?

I'm writing a code with selenium.
My code should open YouTube, input a word, click on search button and open a video.
Everything except the last one. I can't open a video. Could you please help me?
My code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[#id="search-icon-legacy"]')
searchbutton.click()
elements = driver.find_element(By.XPATH, '//*[#id="video-title"]/yt-formatted-string').click()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
wait = WebDriverWait(driver, 20)
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[#id="search-icon-legacy"]')
searchbutton.click()
video_titles_xpath = "//*[#id='video-title']/yt-formatted-string"
wait.until(EC.visibility_of_element_located((By.XPATH, video_titles_xpath)))
video_titles = driver.find_elements(By.XPATH, video_titles_xpath)
video_titles[0].click()

Unable to locate nCino element through Selenium in Salesforce

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.

Both of the input box has exactly same attributes. How do I take unique xpath?

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");
}
}

Unable to click on particular element. I have tried locating object using xpath, css, creating your own xpath and css but none of the is working

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();

How to use WebDriver Wait in IE?

I have Home Page, from the if I click Login, it will be navigated to another page (there i have to enter login credentials to login the account.
Without the below piece of Code, it is perfectly working for Firefox and Chrome.. But IE it is not working.. I assumed to add wait so that IE problem will resolve..
WebDriverWait Test_Wait=new WebDriverWait(driver,10);
WebElement click=Test_Wait.until(ExpectedConditions.elementIfVisible("login_xpath"));
I have added this code.. But I am getting error to change elementIfVisible to elementClickable.. If i change to elementClickable. Again it is giving error to change elementIfVisible.. How to resolve this??
package com.test.testCase;
import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.test.utitlity.clickEvent;
import com.test.utitlity.globalVariables;
import com.test.utitlity.switchWindow;
public class driverManager extends globalVariables{
public static void driverManager(){
browser="ie";
if(browser.equals("ie")){
File file = new File("./IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getPath());
driver= new InternetExplorerDriver();
}
else if (browser.equals("firefox"))
{
driver=new FirefoxDriver();
}
else if (browser.equals("chrome"))
{
File file= new File("./chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getPath());
System.out.println(file.getPath());
driver=new ChromeDriver();
}
driver.get("http://www.xxxx.in");
clickEvent.clickAt("login_xpath");
WebDriverWait Test_Wait=new WebDriverWait(driver,10);
WebElement click=Test_Wait.until(ExpectedConditions.elementIfVisible("login_xpath"));
switchWindow.swindow("TST.");
}
}
From the code as I can see, you are using it incorrectly. First, you have to wait for the element to be visible and then you have to click on it but you are using it the other way around, and hence it is not able to find the login button as it has been clicked and you are in the new page already.
Please use the wait like this. This will work in all browsers (IE, always a hindrance, but it will work nevertheless):
try{
WebDriverWait Test_Wait=new WebDriverWait(driver,20);
WebElement click=Test_Wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("login_xpath")));
clickEvent.clickAt("login_xpath");
}catch(Throwable e){
System.err.println("The login element is not found");
}

Resources