简体   繁体   English

Python - Selenium和XPATH从表中提取所有行

[英]Python - Selenium and XPATH to extract all rows from a table

I am using Selenium and XPATH to extract all rows from a table, but can only get the first row. 我使用Selenium和XPATH从表中提取所有行,但只能获取第一行。

Here is what I am doing: 这是我在做的事情:

from selenium import webdriver

path_to_chromedriver = '/Users/me/Desktop/chromedriver'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)

url = "http://www.psacard.com/smrpriceguide/SetDetail.aspx?SMRSetID=1055"

browser.get(url)
browser.implicitly_wait(10)

SMRtable = browser.find_element_by_xpath('//*[@class="set-detail-table"]/tbody')

for i in SMRtable.find_element_by_xpath('.//tr'):
    print i.get_attribute('innerHTML')

browser.close()

The SMRtable variable has all the rows in it when I convert to string and print. 当我转换为字符串并打印时, SMRtable变量包含其中的所有行。 When I try to loop through it, it throw a not iterable error. 当我尝试循环它时,它会抛出一个not iterable错误。

I also tried using browser.find_element_by_xpath('//*[@class="set-detail-table"]/tbody/tr') , but this only gives me the first row. 我也尝试过使用browser.find_element_by_xpath('//*[@class="set-detail-table"]/tbody/tr') ,但这只给了我第一行。 I tried adding [position()>0] after /tr , but still got just the first row. 我尝试在/tr之后添加[position()>0] ,但仍然只有第一行。

How can I get all of the rows? 我如何获得所有行?

You need find_elements_by_xpath() (watch the "s") instead: 你需要find_elements_by_xpath() (注意“s”):

for i in SMRtable.find_elements_by_xpath('.//tr'):
    print i.get_attribute('innerHTML')

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

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