-1

I just started using web driver using C#. I am having issue to find that image element to click on it. Below is the HTML for that. Would be great if anybody can help me out. Thanks.

<a id="3245248" class="detail-info" href="#">
<img title="Order Information" src="/Content/images/24x24/info.png">
dee-see
  • 21,841
  • 5
  • 54
  • 87
user1753462
  • 9
  • 1
  • 3
  • Do you mean [selenium web driver](http://code.google.com/p/selenium/wiki/GettingStarted)?? – huMpty duMpty Dec 31 '13 at 15:52
  • Yes. Selenium webdriver. I guess, it would be easy to find that element using CssSelector but I am not good in it. – user1753462 Dec 31 '13 at 15:53
  • @user1753462: Ok, can you post the code you used to get the image element? You should have a `id` or a `class` in `img` – huMpty duMpty Dec 31 '13 at 15:54
  • [FindsBy(How = How.CssSelector)] public static String browse_close_Info_CSS = "#3245247.detail-info"; IWebElement browse_close_Info = driver.FindElement(By.CssSelector(browse_close_Info_CSS)); Hpages.commonmethods().MouseovertoSearchAndClick(browse_close_Info); – user1753462 Dec 31 '13 at 15:55
  • That code would do better in your question. Please edit your question to add it. – John Saunders Dec 31 '13 at 15:58

3 Answers3

1

Assuming your webdriver mentioned is the Selenium WebDriver Nuget package, and assuming your anchor tag has a closing tag after your img tag, you should be able to use Selenium to select the img tag like this:

IWebDriver driver; //previously instantiated.
driver.FindElement(By.CssSelector("#3245248 img")).Click();

Alternatively, just click on the anchor tag itself:

IWebDriver driver;
driver.FindElement(By.Id("3245248")).Click();

The trick here is to understand CSS selectors. # precedes an id selector and putting a space and tag name after that is a child selector. So in short, select the anchor tag by id and look inside it for an img tag.

Another helpful tip to understand with Selenium, if you can select it using your web browser's JavaScript console by calling document.querySelector('some css selector') then Selenium will be able to select it as well.

If this does not help, please update your question to be more specific.

Adam Venezia
  • 2,411
  • 2
  • 13
  • 10
0

You'll want to click the anchor tag, rather than the image itself. You can try this XPath to find the anchor tag if the id is unique and not going to change

"//a[@id='3245248']"

Or a little safer, if the ID is dynamic, find the anchor tag that has your image inside it:

"//a[./img[@title='Order Information']]"

Or this CSS Selector, again, only if the ID is unique and not going to change

"a#3245248"

EDIT: Use this rather than the FindsBy annotation that I think you're using, which is susceptible to break if an element is dynamically added/modified after the page loads

IWebElement link = driver.FindElement(By.XPath("//a[./img[@title='Order Information']]"));
link.Click();

If you debug this, you should see whether or not it actually finds the IWebElement first

  • I tried every possible way. It seems if I use Xpath "//a[./img[@title='Order Information']]" it is finding the elements but not clicking on it. And if using Css Selector #3245248 img or only id it is not finding the element. – user1753462 Dec 31 '13 at 16:20
  • This image is inside a container. I am adding total source code.
    – user1753462 Dec 31 '13 at 16:23
  • Does the anchor tag have a closing tag? That would explain the css selector "#3245248 img" not working I think – Steve Weaver Crawford Dec 31 '13 at 16:35
  • Yes it is a closing tag. – user1753462 Dec 31 '13 at 16:37
  • When you say "it is finding the elements but not clicking on it", are you sure it is not clicking on it, or is it just that there is no onclick action defined for that element (ie. It does click, but doesn't appear to do anything)? If the latter is the case then you're clicking the wrong element, and you need to work out which element has the click action bound to it – Steve Weaver Crawford Dec 31 '13 at 16:47
  • I am not sure but assume it is not clicking on it. When I am running in debugging mode, it is not finding element which suppose to open after clicking this image element( as element was not clicked and pop up window did not open to go for next step). But, if using your mentioned Css Selector, it is showing error message for this respective image element only. That is why I assumed it is not finding the element. I am sure the click action working as it is clicking prior steps. – user1753462 Dec 31 '13 at 16:52
  • When I am using XPATH .//*[@id='3245654']showing message "Element not found in the cache - perhaps the page has changed since it was looked up" – user1753462 Dec 31 '13 at 17:43
  • Try without using the FindsBy annotations that I see in another comment. I will edit my answer in a minute – Steve Weaver Crawford Dec 31 '13 at 17:57
  • I tried with hard coded Css Selector without Find By annotation.IWebElement browse_close_Info = driver.FindElement(By.CssSelector("#3245248 img")); browse_close_Info.Click(); – user1753462 Dec 31 '13 at 18:13
  • Without Find By annotation getting error "An invalid or illegal string was specified" – user1753462 Dec 31 '13 at 18:44
  • 1
    Probably caused by what the browser considers an invalid ID. See http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – Steve Weaver Crawford Dec 31 '13 at 18:47
  • I agree but When I am using XPATH .//*[@id='3245654']showing message "Element not found in the cache - perhaps the page has changed since it was looked up". It is not making any sense to me. I am wondering, do I have to use switch function to get into the container as we do for Iframe? – user1753462 Dec 31 '13 at 19:04
  • The fact that you keep posting different IDs for this element probably means that the ID is dynamic, and shouldn't be used to locate an element, since it probably changes each time the page loads. Go back to using the secoond XPath I mentioned originally, and don't use FindsBy notation – Steve Weaver Crawford Dec 31 '13 at 19:22
  • Still the same issue. IWebElement browse_close_Info = driver.FindElement(By.XPath("//a[./img[@title='Order Information']]")); browse_close_Info.Click(); ERROR : "Element not found in the cache - perhaps the page has changed since it was looked up". – user1753462 Dec 31 '13 at 19:55
  • I think you need to reassess what the issue actually is & perhaps ask a new question. Googling that error message there seem to be a number of responses already, mainly concerning iframes. It's hard to tell what's going on with yours without seeing the relevant HTML – Steve Weaver Crawford Dec 31 '13 at 20:34
  • Thanks for all help though. I guess it need switching to the frame. I already posted total related HTML above. I tried all alternative. If I find any other clue, would be posting. – user1753462 Dec 31 '13 at 21:13
  • My Issue has been solved. Thank you for all help. I was bit stupid as I did not follow the behavior of the application when the element is being clicked. It is actually taking little long the form to be stable that is why just had to wait explicit wait before clicking action. – user1753462 Jan 02 '14 at 16:25
0

css=a.detail-info > img[src='/Content/images/24x24/info.png'] This CSS can be used.

Let me know is this CSS Selector is working or not.

user3487861
  • 320
  • 2
  • 2