Good day.

I have a grid on the page, which data can be changed. When you click on a cell, TextArea , ComboBox or a calendar may appear, depending on the cell.

The problem is that the drop-down control does not appear in the grid, but several pairs higher.

This implementation will be used in selenium , and therefore, when you click a particular cell, you need to go up from it to find the desired element.

I do this:

 var parent = element.FindElements(By.XPath("..//..//..//..//..//..//..//..//..//..//*")); 

and further I already look for the necessary control in parent.

Question:

1) How can you better write XPath to climb several levels higher without ..//..//..// , because in some places you need to climb 16 budgets?

2) Is it possible somehow through XPath, in my case to look for a specific control?

for example

 if(element.FindElements(By.XPath("..//..//..//..//..//..//..//..//..//..//textArea[class = 'blabla']")).Count > 0) 

How best to implement such a task?

    2 answers 2

    If I understood correctly, then:

     ancestor::textArea[class = 'blabla'] 

    Although how a textArea can be someone's ancestor is not entirely clear. Maybe so:

     ancestor::*/textArea[class = 'blabla'] 

    Threat For Firefox, there is a Selenium-IDE plugin, where you can find the xpath for an element just by clicking on it.

    • Not exactly. It turns out that selenium clicks on the cell in the grid and the textArea appears, but not in the grid, but higher in the markup. So, as Selenium now sees a specific cell, and not the entire page markup, which changed after the click, you need to go higher on the item by parents to see the whole markup and find the desired textArea. The method you suggested unfortunately does not work. (( - Telsystems
    • @Telsystems, give the markup. According to your description, the second option should work. Better words will tell the code. - Yura Ivanov

    The most elegant of ways, I think, to look for a friend, in which all affiliated elements will appear. And then look for the child elements inside this member, not inside the document.

    Through ".// your xpath" (it is something concrete that you write after "..//..//..//............ ..//..// ")