0

I am trying to finding xpath for image.below is my code.i am getting error unable to locate the element.

driver.findElement(By.xpath("//img[@src='./pics/logo  /home.jpg']")).click();

Below mention is my table code. from where i am trying to find xpath for image.

<table cellspacing="0" cellpadding="0" width="600" border="0">
  <tbody>
    <tr>
      <tr>
        <td style="vertical-align: top;padding-top:10px;padding-right: 3px;">
          <td width="30%" style="vertical-align: top;padding-top:10px;">
            <a title="Access to Data (S,g,...)" target="_top" href="./action/updateTabs?tabSet=requestId=1457516682135">
              <img border="0" src="./pics/logo/home/EMLogoMini.jpg">
            </a>
          </td>
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
Rimjhim
  • 23
  • 4

2 Answers2

1

I have observed that there is a space in your xpath and the URL is different too..

Use below code:-

driver.findElement(By.xpath("//img[@src='./pics/logo/home/EMLogoMini.jpg']")).click();

Or use cssSelector as below :-

driver.findElement(By.cssSelector("img[src='./pics/logo/home/EMLogoMini.jpg']")).click();

    List<WebElement> list=driver.findElements(By.xpath("//img[@src='./pics/logo/home/EMLogoMini.jpg']"));

    for(WebElement e : list){
            e.click();
       }

How to click by different ways:-

If your problem is that the element is scrolled off the screen (and as a result under something like a header bar), you can try scrolling it back into view like this:

private void scrollToElementAndClick(WebElement element) {
    int yScrollPosition = element.getLocation().getY();
    js.executeScript("window.scroll(0, " + yScrollPosition + ");");
    element.click();
}

if you need you could also add in a static offset (if for example you have a page header that is 200px high and always displayed):

    public static final int HEADER_OFFSET = 200;

    private void scrollToElementAndClick(WebElement element) {
    int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET;
    js.executeScript("window.scroll(0, " + yScrollPosition + ");");
    element.click();
}

If still not work then use JavascriptExecutor

WebElement element= driver.findElement(By."Your Locator"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • I tried it is throwing same error. Below is the mentioned error. – Rimjhim Mar 09 '16 at 10:29
  • Unable to locate element: {"method":"css selector","selector":"img[src='./pics/logo/home/EIMLogoMini.jpg']"} Command duration or timeout: 2.09 seconds – Rimjhim Mar 09 '16 at 10:31
  • have you use implicit wait? ... do you have any frame in your DOM? .. is there any other img tag is present in your DOM having the same src value – Shubham Jain Mar 09 '16 at 10:32
  • use this line of code after driver.get(); -> driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); – Shubham Jain Mar 09 '16 at 11:18
  • and there is two image for same source. do u have any solution – Rimjhim Mar 09 '16 at 11:20
  • Is it `EMLogoMini.jpg` or `EIMLogoMini.jpg`? These locators are extremely sensitive, so you should always copy/paste and never type anything yourself, even for very simple strings. – Kim Homann Mar 09 '16 at 11:59
  • you are right.. it's a good catch .... thanks @KImHomann ... Rimhjim I have updated my answer with change .. please check now – Shubham Jain Mar 09 '16 at 12:53
  • it is EIMLogoMini.jpg i mentioned correct only. still it is not working – Rimjhim Mar 10 '16 at 05:18
  • are you able to click now? ... are you getting the same error ... if you are still getting the same error then there should be frame present on you DOM .. to handle that frame refer :- http://www.seleniumeasy.com/selenium-tutorials/how-to-work-with-iframes-in-selenium-webdriver – Shubham Jain Mar 10 '16 at 05:23
  • now i am not getting error. But Click() method is not working.it is not redirecting to other page.:-( – Rimjhim Mar 10 '16 at 05:27
  • is it working manually? ... if yes then I have added few ways to click on element in my answer .. have a try – Shubham Jain Mar 10 '16 at 05:33
  • try -> //a[@title='Access to Engineering Data (Smaragd, EDS/BCS, DIALOG, ZGDOK,...)'] – Shubham Jain Mar 10 '16 at 05:42
  • and this -> //a[@title='Access to Engineering Data (Smaragd, EDS/BCS, DIALOG, ZGDOK,...)']/img – Shubham Jain Mar 10 '16 at 05:42
  • Hi @ShubhamJain I analysis and in home page there is a frame inside it 3 more frame.can u help how we can handle it. Below is a code. and frame which i need to handle that is name with "content". inside that we have image and table. – Rimjhim Mar 11 '16 at 09:58
  • refer my answer here http://stackoverflow.com/questions/35170447/selenium-in-c-sharp-how-do-i-navigate-different-frames – Shubham Jain Mar 11 '16 at 10:06
  • try this first -> driver.switchTo().frame("frameset"); – Shubham Jain Mar 11 '16 at 10:06
  • @ShubhamJain. now it is working fine thanks for the answer. and i am new for selenium could you please guide me any site which is good for selenium beginners – Rimjhim Mar 14 '16 at 08:30
  • cool .. it feel good to know that your issue is resolved ,... refer you site :- http://toolsqa.com/ .............. http://www.guru99.com/selenium-tutorial.html ................... – Shubham Jain Mar 14 '16 at 08:33
1

try to remove spaces and be sure that the URL is same...

Fazz
  • 101
  • 10