-1

I am writing a selenium script that automates a web-page. I need to click on a button which is defined within a list.

This is the image of my web UI - New Account is the button I am referring to

Image

This is my XML code :

<div id="00B4E000000LQ2C_topNav" class="topNav primaryPalette">
  <div id="00B4E000000LQ2C_subNav" class="subNav">
    <div class="linkBar brandSecondaryBrd">
      <div id="00B4E000000LQ2C_listButtons" class="listButtons">
        <ul class="piped">
          <li>
            <input class="btn" type="button" title="New Account"       onclick="navigateToUrl('/setup/ui/recordtypeselect.jsp?ent=Account&ekp=001&retURL=%2F001%3Ffcf%3D00B4E000000LQ2C%26isdtp%3Dnv%26nonce%3Df8007ad94993912b7ff4149193a6096ccfed4ebb1454e0b9b310ad14b61de71d%26sfdcIFrameOrigin%3Dhttps%253A%252F%252Fcs83.salesforce.com&save_new_url=%2F001%2Fe%3FretURL%3D%252F001%253Ffcf%253D00B4E000000LQ2C%2526isdtp%253Dnv%2526nonce%253Df8007ad94993912b7ff4149193a6096ccfed4ebb1454e0b9b310ad14b61de71d%2526sfdcIFrameOrigin%253Dhttps%25253A%25252F%25252Fcs83.salesforce.com&isdtp=vw','LIST_VIEW','new');" name="new" value="New Account"/>
          </li>
          <li class="lastItem">
        </ul>

I used:

driver.findElement(By.xpath(".//*[@id='00B4E000000LQ2C_listButtons']/ul/li[1]/input")).click();

(Xpath was given by the firebug) but it gives me an error stating

unable to locate elements

Please help me script / locate this button.

zx485
  • 28,498
  • 28
  • 50
  • 59
Chaitanya
  • 1
  • 2

2 Answers2

1

You don't have to use XPaths generated by the Firebug and check the element's parents along the way. We can do better, you can write a more reliable and a simpler way to locate the element:

driver.findElement(By.name("new"));

or:

driver.findElement(By.cssSelector("input[name=new]"));

or:

driver.findElement(By.cssSelector("input[value='New Account']"));

Note that the XPath expression you have looks valid. You may be experiencing a timing issue and would need to wait for the element presence, visibility or clickability, see: How to wait until an element is present in Selenium?.

And, if the button is inside the iframe, you need to switch to its context and only then search the button:

driver.switchTo().frame("ext-comp-1005");
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • You mean `('input[name="New Account"]')`. The `value` obviously changes as the user enters it. – Narxx Apr 12 '16 at 15:08
  • 1
    @Narxx well, I bet the user would not be able to change the button's value :) – alecxe Apr 12 '16 at 15:08
  • Thanks for ur reply . I tried . Still the same error Unable to locate the element – Chaitanya Apr 12 '16 at 15:18
  • @Chaitanya have you tried adding a wait with `WebDriverWait`? Also, check if this element is inside an iframe or not. Thanks. – alecxe Apr 12 '16 at 15:24
  • Hi alecxe, The button is inside the i-frame , it mentions - iframe#ext-comp-1005 . How do I proceed with this ? – Chaitanya Apr 12 '16 at 15:38
  • @Alecxe - It worked ! Thank you very much for your help :) – Chaitanya Apr 12 '16 at 15:49
  • @alecxe : It gives me this error - Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Comand . I ahve increased the time duration but I still find the same error . Could you tell me how do I get rid of this , Please. – Chaitanya Apr 14 '16 at 08:58
0

Hi please try like below

// first way 
driver.findElement(By.xpath("//*[@name='new']")).click();
// second way 
driver.findElement(By.xpath("//*[@class='btn']")).click();
// basically you can use various attributes of input tag with button inside the xpath to click

Update working with i frame

// A short way to identify how many iframe's are present on a web page

List<WebElement> numberOfFrames= driver.findElements(By.tagName("iframe"));
System.out.println("Total Number of iframes present are : " +numberOfFrames.size());
for(int i=0;i<numberOfFrames.size();i++){
    // here u can identify iframes with any of its attribute vale say name,title or which is most suitable.
    System.out.println("Name of the i-frames : " + numberOfFrames.get(i).getAttribute("name"));
}

// Back to your question - button lies inside iframe hence
// key here is before clicking you have to switch to the frame first

driver.switchTo().frame(driver.findElement(By.name("frame name")));

hope this helps you

Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
  • Thanks for ur reply . I tried . Still the same error Unable to locate the element – Chaitanya Apr 12 '16 at 15:19
  • Strange .are you getting unable to locate element error in the console ? if yes are you sure the button which you want to click is not inside any i-frame.also to add have you managed synchronization of the code if not please use link provided by @ alecxe – Rajnish Kumar Apr 12 '16 at 15:21
  • Yes, I am getting error in the console. I guess the button I want to click is inside i-frame. Can you see the image attached ? left most portion near xpath mentions about i-frame. Can you please help me with this . – Chaitanya Apr 12 '16 at 15:35
  • Hi Rajnish, Yes- Button is inside an iframe. It mentions iframe#ext-comp-1005 input[name="new"] . How do i add synchronisation for the code ? Didn't find the link which @alecxe provided – Chaitanya Apr 12 '16 at 15:40
  • hi i have updated my answer please have a look at it it will solve your problem – Rajnish Kumar Apr 12 '16 at 15:44
  • Thanks Rajnish for your help :) – Chaitanya Apr 13 '16 at 08:38
  • @ raj N ish Ku M ar - It gives me this error - Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Comand . I ahve increased the time duration but I still find the same error . Could you tell me how do I get rid of this , Please. – Chaitanya Apr 14 '16 at 08:59
  • plz try this it will help http://stackoverflow.com/questions/20903231/selenium-wait-until-element-is-present – Rajnish Kumar Apr 14 '16 at 09:30