1

I am trying to get the id of an element which starts with 'u_' and ends with '_5' and in between data changes alphabetically.So, sometimes i see 'u_d_5' and sometime 'u_6_5' . In that case, how would get the dynamic id? I know there are some methods like starts-with or ends-with but they aren't working at all for me. Could anyone please suggest me some idea in this? code snippet:

WebElement cls=ff.findElement(By.xpath("//*[@id='u_6_5']"));

whole code:

    String s=System.getProperty("user.dir");
    System.setProperty("webdriver.chrome.driver", s+"\\ChromeDriver\\chromedriver.exe");
    ChromeDriver ff=new ChromeDriver();
try{
    ff.manage().window().maximize();
    ReadfrmExcel rd=new ReadfrmExcel();//reading data
    String[][] readata=rd.excelRead();
    for(int i=1; i<readata.length; i++)
    {
    ff.get("http://www.facebook.com");
    Thread.sleep(1000);
    ff.findElementByXPath("//*[@id='email']").sendKeys(readata[i][0]);
    ff.findElementByXPath("//*[@id='pass']").sendKeys(readata[i][1]);
    ff.findElement(By.id("u_0_v")).click();
    System.out.println("Waiting for element to appear");
    Thread.sleep(3000);
    WebElement element=ff.findElement(By.id("userNavigationLabel"));
    element.click();
    System.out.println("Clicked drop down");
    //ff.findElementByXPath("//*[@id='userNavigationLabel']").click();
    System.out.println("sleeping 5 secs");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        WebElement cls=ff.findElement(By.xpath("//*[starts-with(@id,'u_')   and ends-with(@id, '_5']"));
    boolean vis=cls.isDisplayed();

    System.out.println(vis);
    Thread.sleep(8000);
    System.out.println("Sleeping 8 secs");
    cls.click();
    System.out.println("After clicking");

    System.out.println("Successfully logged out!!");
    //ff.close();
    ff.close();

    }
    }
    catch(Exception e){
        System.out.println(e);
    }
lss mesy
  • 59
  • 2
  • 8

2 Answers2

2

you can use xpath's starts-with and ends-with methods:

WebElement cls=ff.findElement(By.xpath("//*[starts-with(@id,'u_') and ends-with(@id, '_5')]"));

if this is not working, your browser might only support xpath 1.0 (see this answer for details) -> then you can only use starts-with

WebElement cls=ff.findElement(By.xpath("//*[starts-with(@id,'u_')]"));

if you definitely need to check the end of the id, then you can try the following xpath instead:

//*[starts-with(@id, 'u_') and substring(@id, string-length(@id) - 1) = '_5']
Community
  • 1
  • 1
drkthng
  • 6,651
  • 7
  • 33
  • 53
  • thanks @drkthng..Trying your solution..will update soon – lss mesy Nov 03 '15 at 12:48
  • didn't work out though :-( ..throws me no such element error. I've updated my question will full code. Please have a look and let me know what am i missing ! – lss mesy Nov 03 '15 at 12:57
  • are you sure the element is already there when you try to find it? can you post the html of that moment? cheers! – drkthng Nov 03 '15 at 13:07
  • Yes..It's always there but its id gets changed all the time. Moreover, i wrote a script to login-logout from Facebook, so for 'Log out', its ID always gets changed each time. Hope you understood my problem ! thanks ! – lss mesy Nov 03 '15 at 13:19
  • @lssmesy might be a problem of your browser not supporting xpath 2.0 -> updated my answer – drkthng Nov 03 '15 at 14:03
0

I have tried to get all ids, which i have to use. First get the tag name in which your id attribute has been defined.

java.util.List links = ff.findElements(By.tagName("span"));

ArrayList arrList = new ArrayList();

for (int i = 0;i < links.size();i++){

   if(links.get(i).getAttribute("id").startsWith("u_") && 
          links.get(i).getAttribute("id").endsWith("_5")){
                arrList.add(links.get(i).getAttribute("id"));
            }
        }

  Now you will find all the ids which are starts with "u_" and ends with"_5".

 System.out.println("arrList elements : "+arrList);