简体   繁体   中英

relative xpath from webelement to another webelement

so I am trying to get from one webelement to another webelement by using XPath. So this is the path where the webelement is located, which I find by using "findElementsByClassName".

//*[@id="appView2"]/div/div[3]/div/table/tbody[3]/tr[2]/td[2]/div/div[2]/div/div/div

And this is where I need to get to.

//*[@id="appView2"]/div/div[3]/div/table/tbody[3]/tr[2]/td[2]/div/div[1]/span

I can't just point to it directly since the column and row change for each web element.

I have tried a couple of different paths like:

.//.//.//.//div[1]/span

But it can't find the element.

If someone got a better solution, that would also work.

Thanks!

You really shouldn't rely on indexes like this (eg div[3] or tbody[3]). It makes your implementation brittle which will break with the slightest change to the html. Instead of

//*[@id="appView2"]/div/div[3]/div/table/tbody[3]/tr[2]/td[2]/div/div[2]/div/div/div

You could do something like

//*[@id="appView2"]//table[@class='xxx']//div[text()='xxx']

In answer to your original question, instead of

.//.//.//.//div[1]/span

It will be something like

../../../../div[1]/span

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM