Why web elements lie in just one element of a list?

python-2.7 list selenium xpath append

75 观看

1回复

407 作者的声誉

I crawled data from Scopus website and saved it in the list named "s". But all of the data have been saved in s[1]. My code is:

s=[]
document=driver.find_elements_by_xpath("""//*[@id="srchResultsList"]/tbody""")
for i in document:
    s.append(i.text)

What should I do? I used selenium package in python 2.7.

作者: hamed baziyad 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

1175 作者的声誉

决定

I noticed that you are trying to query a tbody table tag. It it not the reason? Generally there is only a tbody tag inside a table, and it results in a unique element.

You didn't told us if you are trying to return and save in your var all tr elements, or all td elements, or even another kind of tag inside of they, but whatever you are trying to return, you need to change your selector.

Let's imagine that your xpath returns a tbody element with a ton of IWebElements inside of it: tr, td, a, span... But your selector is asking only for the tbody element, not the inside elements, do you get it?

just try to change your xpath to //*[@id="srchResultsList"]/tbody/tr if you want all tr elements exept headers tr;

You can also use the following xpath //*[@id="srchResultsList"]/tbody//td that will return all td elements that you want.

In another case, if you want all tr AND td elements inside tbody, you can query that way: //*[@id="srchResultsList"]/tbody/tr | //*[@id="srchResultsList"]/tbody//td

You can also change or create subqueryes to return all elements what you desire for, but you need to specify it on selector: selenium just accepts the imediate result without children

作者: Striter Alfa 发布者: 2017 年 12 月 27 日
32x32