Using xpath unable to select dynamic id

Followed your tutorial of xpath and generated a relative xpath for the gmail – compose window -attach icon .  ” driver.findElement(By.xpath(“//div[contains(@id,’:1′)]”)).click();
where the id of the attach file is keep on changing except first two characters ” :1″ .
Please help me .

Default Asked on November 7, 2014 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)

    If you are talking specifically about Gmail compose button then instead of ‘contains’ strategy , can you try using this
    //div[@class=’a1 aaA aMZ’]

    Supporter Answered on November 7, 2014.

    Hi Kapil,
    Thanks for the reply, with class its working. But I am trying to learn ” how to handle dynamic id ” thats why i choose gmail – attach icon section .
    Can you help me to identify why we are not able to use Id here .

    on November 7, 2014.

    Yes sure ,probably this is not the best candidate for this exercise as you can see if you have dynamic ids and want to use ID as a locator then you need to have some static part also with idexample would be id01,id02,id03 here the dynamic part is 01 and 02 ,03 and i can say contains(@id,’id’) , but in case of attach button in gmail only static text in the id is “:” as per the rule we have locator as //div[contains(@id,’:’)] and you can try to see how many elements it will return – around 411which is not what we want , hence chose some other element

    on November 7, 2014.

    Hi Kapil,

    Welcome to ToolsQA.

    Lakshay Sharma

    on November 7, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.