使用python硒无法找到并单击网页上的链接

时间:2019-08-04 12:40:54

标签: python-3.x selenium-webdriver selenium-chromedriver

我正在尝试从this网站开始抓取数据。我要:

  1. 从人口普查下拉列表中选择“ 2001”
  2. 点击“主要普查摘要”
  3. 点击“主要普查摘要总数”
  4. 反复浏览“状态”下拉菜单中的选项。

我的问题在第2步和第3步中。我根本无法单击“主要普查摘要”。我已经尝试过

driver.find_element_by_link_text('Primary Census Abstract').click()

driver.find_element_by_id('ctl00_MainContent_lnk5').click()

的几种变体

driver_find_element_by_xpath(...)。click()

使用

// [@ class ='dvbooks'] // [text()='主要人口普查摘要']和 // * [@@ class ='dvbooks'] / li [5] / a

每次我遇到相同的错误

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素。我有什么想法我做错了吗?

到目前为止,这是我的代码。

import os
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC   

# Initiate Chrome Driver
chromedriver = 'Path to Chromedriver'
chrome_options = Options()
chrome_options.binary_location = 'Path to Google Chrome'
driver = webdriver.Chrome(chromedriver, options=chrome_options)

# Open Search Page
driver.get('http://censusindia.gov.in/DigitalLibrary/Tables.aspx' )

# Select Year
dropdown_year = Select(driver.find_element_by_id('ctl00_MainContent_ddlYear'))
dropdown_year.select_by_visible_text('2001')
WebDriverWait(driver, 20).unitl(EC.presence_of_element_located(By.ID, "ctl00_MainContent_lnk5")))

# Click PCA
button = driver.find_element_by_xpath("//*[@class='dvbooks']//*[text()='Primary Census 
Abstract']")
button.click()

1 个答案:

答案 0 :(得分:0)

此代码单击“主要人口普查摘要”元素,“主要人口普查摘要总数”元素,然后从所选页面的下拉列表中选择状态。

注意:此代码不会下载每种状态下列出的普查文件。

import time
from random import randint

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC 

# Initiate Chrome Driver
chrome_options = Options()
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

# Open Search Page
driver.get('http://censusindia.gov.in/DigitalLibrary/Tables.aspx' )

# Select Year
dropdown_year = Select(driver.find_element_by_id('ctl00_MainContent_ddlYear'))
dropdown_year.select_by_visible_text('2001')

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.LINK_TEXT, 'Primary Census Abstract')))

primary_census_abstract = driver.find_element_by_xpath("//*[@id='ctl00_MainContent_lnk5']")
primary_census_abstract.click()

census_abstract_total = driver.find_element_by_xpath("//*[@id='ctl00_MainContent_lnkPCA1']")
census_abstract_total.click()

list_of_states = ['India', 'Andaman and Nicobar Islands', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam',
              'Bihar', 'Chandigarh', 'Chhattisgarh', 'Dadra and Nagar Haveli', 'Daman and Diu', 'Delhi',
              'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jammu and Kashmir', 'Jharkhand',
              'Karnataka', 'Kerala', 'Lakshadweep', 'Madhya Pradesh', 'Maharastra', 'Manipur', 'Meghalaya',
              'Mizoram', 'Nagaland', 'Orissa', 'Puducherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu',
              'Tripura','Uttar Pradesh']

for state in list_of_states:
  select_state = driver.find_element_by_xpath("//*[@id='ctl00_MainContent_ddlpca1state']")
  select_state.click()
  selected_state = Select(select_state)
  selected_state.select_by_visible_text(state)

  ###################################################
  # Add your code to download the census files here
  ###################################################

  time.sleep(randint(5, 10))

  # navigates back to the previous page
  driver.execute_script("window.history.go(-1)")
相关问题