Indexing Selenium Results

I am trying to index the results returned by xpath. For instance:

xpath = '//a[@id="someID"]'

may return multiple results. I want to get their list. I thought what to do:

numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
   l.append(sel.get_text('(%s)[%d]'%(xpath, i)))

will work because something like this with firefox Xpath checker works:

(//a[@id='someID'])[2]

returns the second result.

Ideas Why Behavior Will Be Different And How To Do It With Selenium Thanks

+3
source share
3 answers

The answer is that you need to tell selenium that you are using xpath:

numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
   l.append(sel.get_text(xpath='(%s)[%d]'%(xpath, i)))
+1
source

xpath /html/descendant::a[@id="someID"] /html - , , id('content'). , [1], [2] ..

XPath TR http://www.w3.org/TR/xpath#path-abbrev:

. //para[1] , /descendant::para[1]. --; -, .

+2

Selenium ,

numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
   l.append(sel.get_text('%s[%d]'%(xpath, i)))

XPath Selenium, //a[@id='someID'][2]

0

Source: https://habr.com/ru/post/1725956/


All Articles