1

Working with automated testing, I have come across the following issue quite a lot of time: I want to find an element on the page, but the element has to be at a specific region of the page.

Take the following as an example: I have a searchfield with type-ahead on the site. In my sidebar, I have the element I am seraching for (lets call it "Selenium"), but that is not the element I am interested in, I want to see if my type-ahead search is delivering the expected result when searching for "Selenium".

<body>
   <header>
      <searchfield>
         <searchresults>
            <a .... >Selenium</a>
         <searchresults>
      </searchfield>
   </header>
   <aside>
      ...
      <a .... >Selenium</a>
      ...
   </aside>
</body>

If I in selenium webdriver search for the linktext "Selenium" it will find both entries, the one in the sidebar aswell as the one in the searchfield. Furthermore am I not able to wait for the searchresult with the WaitForVisible command on the linkText as Selenium will find the element in the sidebar and conclude that the element is preset.

So my question is: "With selenium webdriver, how do I search for an element within a specific region?"

Squazz
  • 3,912
  • 7
  • 38
  • 62

1 Answers1

1

Poking around with this issue, I came across the use of Xpath. With this I could create "areas" where I want to search for an element. As an example, I went from

html/body/div/aside/div[2]/ul/li

to

//div[contains(@class,'coworkerwidget')]/ul/li

Now the code is MUCH more dynamic, and less prone to errors if our frontend guys edit something in the future.

Regarding the search, I could now set up something like the following code

//div[contains(@class, 'searchfield')]//div[contains(@title, 'searchfield') and contains(., '" + searchword + "')]"

First we specify that we want to look in the searchfield area:

//div[contains(@class, 'searchfield')]

I can then set some more criteria for the result I want to find:

//div[contains(@class, 'title') and contains(., '" + searchword + "')]

Some litterature on the subjects for further reading.

http://www.qaautomation.net/?p=388

http://www.w3schools.com/xsl/xpath_syntax.asp

Click a button with XPath containing partial id and title in Selenium IDE

Retrieve an xpath text contains using text()

Community
  • 1
  • 1
Squazz
  • 3,912
  • 7
  • 38
  • 62