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

1.3k Views Asked by At

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!

1

There are 1 best solutions below

3
On BEST ANSWER

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()