开发者

xPath last select element

开发者 https://www.devze.com 2023-04-12 03:54 出处:网络
Can someone help me to bring this code working? I have several select fields and I only want the last one in my v开发者_C百科ariable.

Can someone help me to bring this code working? I have several select fields and I only want the last one in my v开发者_C百科ariable.

variable = browser.elements_by_xpath('//div[@class="nested-field"]//select[last()]

Thanks!


This is a FAQ: The [] operator in XPath has higher precedence (priority) than the // pseudo-operator. This is why brackets must be used to change the default operator priorities. There are at least several similar questions with good explanations -- search for them and read and understand.

Instead of:

//div[@class="nested-field"]//select[last()] 

Use:

(//div[@class="nested-field"]//select)[last()] 


is the class attribute an exact match? if the mark up is like this

<div class="nested-field other">
...

then you'll have to either match by the exact class or use xpath contains.

0

精彩评论

暂无评论...
验证码 换一张
取 消