Cant we use starts-with method for City dropdown of ICICI website | Selenium Forum
P
Pulkit Malviya Posted on 20/11/2018
Hi Ashish,

As u haven't talked about startswith method in video 15 for City dropdown of ICICI Car loan. Can't we use "(By.xpath("//*[starts-with(@id, '1542691')]")).sendKeys("Mumbai")" for entering the City name? if not then why? because i have tried the same but still getting No such element exception. Below is my code till City field-

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class ICICILoan {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Gourav\\chromedriver.exe");
WebDriver d=new ChromeDriver();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.get("https://loan.icicibank.com/asset-portal/auto-loan/check-eligibility?WT.mc_id=Desk_NLI_ProductPage_ApplyNow");
d.findElement(By.xpath("//*[@id='check-eligibility-page']//div[4]//div//div//form//div[2]//div[1]//div//div//button")).click();
d.findElement(By.xpath("//*[@id='check-eligibility-page']/div[4]/div/div/form/div[2]/div[1]/div/div/div/ul/li[2]/a")).click();
d.findElement(By.xpath("//*[@id='mobileNumber']")).sendKeys("9769116780");
d.findElement(By.xpath("//*[@id='1542687784923']")).click();
d.findElement(By.xpath("//*[starts-with(@id, '1542691')]")).sendKeys("Mumbai");
String s=d.getTitle();
System.out.println(s);
System.out.println();
//1542691071937

}
}

Thanks,
Pulkit Malviya

P
Pulkit Malviya Replied on 22/11/2018

Sorry, just a little change in my question, it was mentioned in Module 14, Part 2 not in Module 15.


A
Ashish Thakur Replied on 27/11/2018

Ok...
is id always starting with 1542691


P
Pulkit Malviya Replied on 28/11/2018

yes..its always starting with 1542691, but it still not able to find that element when i am using starts-with method.


A
Ashish Thakur Replied on 28/11/2018

May be you need to wait for element


P
Pulkit Malviya Replied on 29/11/2018

No, from wait also its not working.. below is the code-


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class ICICILoan {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Gourav\\chromedriver.exe");
WebDriver d=new ChromeDriver();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.get("https://loan.icicibank.com/asset-portal/auto-loan/check-eligibility?WT.mc_id=Desk_NLI_ProductPage_ApplyNow");
d.findElement(By.xpath("//*[@id='check-eligibility-page']//div[4]//div//div//form//div[2]//div[1]//div//div//button")).click();
d.findElement(By.xpath("//*[@id='check-eligibility-page']/div[4]/div/div/form/div[2]/div[1]/div/div/div/ul/li[2]/a")).click();
d.findElement(By.xpath("//*[@id='mobileNumber']")).sendKeys("9769116780");
d.findElement(By.xpath("//*[@id='1542687784923']")).click();
Thread.sleep(5000);
d.findElement(By.xpath("//*[starts-with(@id, '1542691')]")).sendKeys("Mumbai");
Thread.sleep(5000);
String s=d.getTitle();
System.out.println(s);
System.out.println();
//1542691071937

}
}


A
Ashish Thakur Replied on 29/11/2018

As I went through your code. It is not working and it seems that you are using browser generated XPath. Please generate your own custom XPath which doesn't contain randomly generated values.
PS numbers are usually random. Try using other attributes.
I tried your code and it is not working because the id is changing in real-time.
Try using the debugger. in real-time and try the XPath you are using in the browser.