简体   繁体   中英

Python Selenium - How do you click on every row element for column of web table?

This may be a two part question so forgive me if this isn't well written:

I'm trying to get my WebDriver to go to the nba team stats page: http://stats.nba.com/teams/traditional/#!?sort=W_PCT&dir=-1

Table Image for Reference

and then click the linked FGM numbers in the table. This should open a new tab in the background. So far I'm completely lost how I should be approaching this.

I'm thinking:

  • I need a way to count the rows
  • Then I need to tell the WebDriver to loop/click through elements in the FGM column for the row count

I'm sure I can get the data with BeautifulSoup but I'm trying to practice with Selenium and working with tables/links that can vary in length. Any leads are appreciated. Thanks!

Python example:

from selenium import webdriver
from selenium.webdriver import ActionChains
web_driver = webdriver.Firefox()

web_driver.get("http://stats.nba.com/teams/traditional/#!?sort=W_PCT&dir=-1")

actions = ActionChains(driver)
team_links = webdriver.find_elements_by_xpath("//body//td[contains(@class, 'first')]/a")

for link in team_links:
    actions.key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()

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