tags:

views:

80

answers:

1

hi,

I am using selenium for testing my application. In my application there are 5 buttons, each have a different image associated with it. I want to click on button which have a specific image associated.

Currently i am using a while loop to get the node of image and then replacing this node into xpath of button to select it.

is there any way either with xpath or css to do this directly. Providing more information-this is like submit button is there and then below this image is there. submit button and images are sibling element and need to click submit button when the next element is specific image

<div class="select">
     <span class="sysTxtBtn submit xxs">
           <span class="btnTagDummy">
     </span>
     <div class="specialRateMarking">
          <img width="79" height="11" alt="Marking2" src="someimages"/>
</div>
<div class="select">
     <span class="sysTxtBtn submit xxs">
           <span class="btnTagDummy">
     </span>
     <div class="specialRateMarking">
          <img width="79" height="11" alt="Marking1" src="someimages"/>
</div>
+1  A: 

Could you include a snippet of your HTML? Below is an example of an image in a form and a few ways of locating it using Selenium, but these may not be relevant depending on your implementation:

<input id="submitForm" name="imgbtn" type="image" src="images/submit.png" />
  • id=submitForm
  • name=imgbtn
  • //input[@src='images/submit.png']
  • //input[contains(@src, 'submit.png')]
  • css=input[src='images/submit.png']

UPDATE:

Given the HTML:

<div class="select">
  <span class="submit">
    <div class="marking1"></div>
    <div class="select">
      <span class="submit">
        <div class="marking2"></div>

You can locate the 'submit' span parent of the 'marking2' div using the following XPaths:

  • //div[@class='marking2']/..
  • //div[@class='marking2']/parent::*
  • //div[@class='marking2']/parent::span

UPDATE 2:

Based on the HTML now included in the question, you can locate the span with the class of submit related to the image many ways, a few examples follow:

  • //div[//img[@alt='Marking2']/span[contains(@class, 'select')]
  • //img[@alt='Marking2']/../../span
  • //div[img[@alt='Marking2']]/preceding-sibling::span

I hope this gives you some ideas. I'd certainly recommend XPath over CSS for locating these elements as it's much better at these sorts of relationships.

Dave Hunt
<div class="select"> <span class="submit"> <div class="marking1"></div><div class="select"> <span class="submit"> <div class="marking2"></div>I need to click on submit for marking 2.Basically we have different items to be selcted and these items may have different markings, based upon offer avilable.The button to select and the markings are different elements.
thomas
I've updated my answer, which hopefully will help you. Your HTML snippet isn't valid HTML though, so if you need further help it'd be good to provide a bit more information - preferably as an edit to your original question. Cheers.
Dave Hunt
Updated again. Hope that helps. Dave.
Dave Hunt
thanks Dave.these worked perfectly
thomas
You're welcome.
Dave Hunt