Forum Discussion

MHealton's avatar
MHealton
New Contributor
3 years ago

Clicking an a tag in the same div as a span containing desired text.

Hey all,

Our org is fairly new to testcomplete, and I have a feeling someone's already nailed down an elegant solution to this type of problem. 

I'm working on building out some regression tests for a form page using Python scripts, and we have several multi-select input elements on this page. Select an item from a dropdown, and it will appear in a list below, along with a link to delete said item:

The quick and dirty version of the html for the list of items is:

 

<div id="container-for-item-list">
  <div id="container-for-first-item-in-list">
    <a href="javascript.removeItem(1);">X</a>
    <span> Item 1</span>
  </div>
  <div id="container-for-second-item-in-list">
    <a href="javascript.removeItem(7);">X</a>
    <span> Item 7</span>
  </div>
</div>

 

What I am hoping to do is create a function that will locate the span containing the text "Item 1", and then click on the <a> tag within the same div, deleting that item. At this point, I'm able to find the span containing the text I want, but am uncertain how to then access and click on the adjacent <a> tag. Any help you have to offer would be greatly appreciated! 

  • Try using FindAll Method to search for "Link". For example,

    function MenuItems()
    {
        var menuitems = Aliases.panelMainMenu.FindAll("ObjectType", "Link", 1);
        for (var i = 0; i < menuitems.length; i++) {
            Log.Message(menuitems[i].Name);
        }
    }

    This will output the Link name.

     

    You can then perform menuitems[i].Click() if the menuitem equals what you want.

  • Hi MHealton.

     

    If you are using xPath to find the span, then you might be able to use the xPath Axes preceding-sibliing to get the a tag.  Not sure exactly if this will work, but something like this added to the end of your xPath for the adjacent <span>.  If not,  try a web search for xPath Axes preceding-sibliing.

     

    YourXPATHCode/preceding-sibling::a[@href='javascript.removeItem(7)'];

     

    I would assume there is some sort of click method for the element once you find it.

     

    I hope this helps, good luck!

     

     

     


     

  • Ewan69's avatar
    Ewan69
    New Contributor

    We can select the text of a span on click with Selenium webdriver. To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname. After identification of the element, we can perform the click operation on it with the help of the click method.

    Mypascoconnect

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      Try using FindAll Method to search for "Link". For example,

      function MenuItems()
      {
          var menuitems = Aliases.panelMainMenu.FindAll("ObjectType", "Link", 1);
          for (var i = 0; i < menuitems.length; i++) {
              Log.Message(menuitems[i].Name);
          }
      }

      This will output the Link name.

       

      You can then perform menuitems[i].Click() if the menuitem equals what you want.