简体   繁体   中英

Get all table elements in python using selenium

I have a webpage which looks like this:

<table class="data" width="100%" cellpadding="0" cellspacing="0">
        <tbody><tr>
            <th>1</th>
            <th>2</th>
            <th>3 by</th>
        </tr>
                        <tr>                    <td width="10%"><a href="foo1">5120432</a></td>
                <td width="70%">INTERESTED_SITE1/</td>
                <td width="20%"><a href="foo2">foo2</a></td>
            </tr>
                        <tr class="alt">                    <td width="10%"><a href="foo1">5120431</a></td>
                <td width="70%">INTERESTED_SITE2</td>
                <td width="20%"><a href="foo2">foo2</a></td>
            </tr>

I want to put somewhere those 2 sites (interested_site1 and interested_site2). I tried doing something like this:

chrome = webdriver.Chrome(chrome_path)
chrome.get("fooSite")
time.sleep(.5)

alert = chrome.find_element_by_xpath("/div/table/tbody/tr[2]/td[2]").text
print (alert)

But I can't find the first site. If I can't do this in a for-loop, I don't mind getting every link separately. How can I get to that link?

使用CSS查询会更容易

driver.find_element_by_css_selector("td:nth-child(2)")

You Can use xpath to deal with this byb looping over each row.

xpath expression : html/body/table/tbody/tr[i]/td[2]

get the number of rows by,

totals_rows =chrome.find_elements_by_xpath("html/body/table/tbody/tr")
total_rows_length = len(totals_rows)

for (row in totals_rows):
    count = 1
    site =  "html/body/table/tbody/tr["+counter+]+"/td[2]"
    print("site name is :"+ chrome.find_element_by_xpath(site).text)
    site+=1

basically loop through each row and get the value in the second column (td[2])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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