简体   繁体   English

从webelement到另一个webelement的相对xpath

[英]relative xpath from webelement to another webelement

so I am trying to get from one webelement to another webelement by using XPath. 所以我试图通过使用XPath从一个Web元素过渡到另一个Web元素。 So this is the path where the webelement is located, which I find by using "findElementsByClassName". 这就是webelement所在的路径,我可以使用“ 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. 我不能直接指向它,因为每个Web元素的列和行都会更改。

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]). 您确实不应该依赖这样的索引(例如div [3]或tbody [3])。 It makes your implementation brittle which will break with the slightest change to the html. 它会使您的实现变得脆弱,而对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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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