Python Selenium - 如何点击Web表列的每一行元素?

时间:2017-09-13 19:01:13

标签: python selenium selenium-webdriver web-testing

这可能是一个两部分的问题,如果写得不好,请原谅我:

我正在尝试让我的WebDriver转到nba团队统计页面:http://stats.nba.com/teams/traditional/#!?sort=W_PCT&dir=-1

Table Image for Reference

然后单击表中链接的FGM编号。这应该在后台打开一个新选项卡。到目前为止,我完全失去了我应该如何接近这一点。

我在想:

  • 我需要一种计算行数的方法
  • 然后我需要告诉WebDriver 循环/单击FGM列中的行数

我确信我可以使用BeautifulSoup获取数据,但我正在尝试使用Selenium并使用长度不同的表/链接。任何线索都表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

Python示例:

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