Python Selenium - 如何点击网页 table 列的每一行元素?
Python Selenium - How do you click on every row element for column of web table?
这可能是一个两部分的问题,如果写得不好,请原谅我:
我正在尝试让我的 WebDriver 转到 nba 球队统计页面:http://stats.nba.com/teams/traditional/#!?sort=W_PCT&dir=-1
Table Image for Reference
然后单击 table 中链接的 FGM 编号。这应该会在后台打开一个新选项卡。到目前为止,我完全不知道应该如何处理这个问题。
我在想:
- 我需要一种方法来计算行数
- 然后我需要告诉 WebDriver
loop/click 通过 FGM 列中的元素进行行计数
我确定我可以使用 BeautifulSoup 获取数据,但我正在尝试使用 Selenium 进行练习,并使用长度可能不同的 tables/links。任何线索表示赞赏。谢谢!
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()
这可能是一个两部分的问题,如果写得不好,请原谅我:
我正在尝试让我的 WebDriver 转到 nba 球队统计页面:http://stats.nba.com/teams/traditional/#!?sort=W_PCT&dir=-1
Table Image for Reference
然后单击 table 中链接的 FGM 编号。这应该会在后台打开一个新选项卡。到目前为止,我完全不知道应该如何处理这个问题。
我在想:
- 我需要一种方法来计算行数
- 然后我需要告诉 WebDriver loop/click 通过 FGM 列中的元素进行行计数
我确定我可以使用 BeautifulSoup 获取数据,但我正在尝试使用 Selenium 进行练习,并使用长度可能不同的 tables/links。任何线索表示赞赏。谢谢!
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()