Selenium导航美国Fact Finder下降

时间:2018-02-26 00:20:03

标签: python-3.x selenium drop-down-menu selenium-chromedriver census

我是Python 3.X的新手,需要编写一个脚本来自动化从American Fact Finder下载美国人口普查数据的过程。我使用selenium webdriver,到目前为止我的代码是:

    driver = webdriver.Chrome(chromePath)

    #make driver navigate to American Fact Finder Download Center
    driver.get('https://factfinder.census.gov/faces/nav/jsf/pages/download_center.xhtml')

    #Make driver click 'Next' to go to Dataset page
    driver.find_element_by_xpath('''//*[@id="nextButton"]''').click()

    #this is where I need to locate the drop down and select American Community Survey'

在“数据集”页面上,我需要从下拉列表中选择“美国社区调查”,但无论我如何尝试找到运行脚本的下拉列表(xpath,id,value等),都会返回{ {1}}

我需要帮助找到正确的元素,然后从下拉菜单中选择“美国社区调查”。

谢谢!

1 个答案:

答案 0 :(得分:0)

我其实能够自己解决这个问题。

对于任何想知道的人来说,错误是由于selenium在页面加载完成之前试图找到该元素而引起的。工作代码是:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

chromePath = r'chromedriver.exe'
driver = webdriver.Chrome(chromePath)

#make driver navigate to American Fact Finder Download Center

driver.get('https://factfinder.census.gov/faces/nav/jsf/pages/download_center.xhtml')
#Make driver click 'Next' to go to Dataset
driver.find_element_by_xpath('''//*[@id="nextButton"]''').click() #needs to be triple quoted
#make driver wait while page loads
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.XPATH, '//*[@id="filterDimensionListId'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print ("Timed out waiting for page to load") 
#Choose ACS 5-year from drop down
driver.find_element_by_xpath('''//*[@id="filterDimensionListId"]/option[2]''').click()