XPath locates sibling elements

program background

Today, when I was working on an automated office program, I found a problem. I want to query [sender city] and [recipient city] under a certain website of the company, but there may be one page or multiple pages. I want to locate The first one, and get the second [span] under the node at the same level, as shown in the figure below.

So I used keywords to locate elements, and it worked!

 (//div//span[contains(text(),"寄方城市")])

insert image description here

Then I succeeded in positioning, but I want to get the second span under the node of the same level

The solution, add the following just fine:

following-sibling::

full version

(//div//span[contains(text(),"寄方城市")])[1]/following-sibling::span

insert image description here

I hope everyone has to help

A little programmer dedicated to office automation

I've seen this, follow + like + bookmark = don't get lost! !

If you want to know more about Python office automation, please pay attention!

Guess you like

Origin blog.csdn.net/weixin_42636075/article/details/131702828