我无法在现有帖子中找到解决方案(虽然我一直在寻找)。我在下拉菜单中进行选择后尝试从代码中的URL中抓取数据。最后,我想点击“保存”按钮并下载excel文件。这是可以正常工作的代码,但最终没有点击保存按钮。
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.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
url = 'http://omms.nic.in/#'
browser = webdriver.Chrome()
browser.get(url)
点击进度监控'菜单中的项目,然后点击“物理和财务项目摘要”'项目。然后我为每个下拉项目做出选择。
progElem = browser.find_element_by_link_text('Progress Monitoring').click()
summElem = browser.find_element_by_link_text("Physical and Financial Project
Summary").click()
browser.implicitly_wait(10)
#select the state
stateElem = browser.find_element_by_xpath("//select[@name='StateCode']")
state_select = Select(stateElem)
ap = state_select.select_by_visible_text('Andhra Pradesh')
#select the district
distElem = browser.find_element_by_xpath("//select[@name='DistrictCode']")
dist_select = Select(distElem)
dist = dist_select.select_by_visible_text('All Districts')
#select the block
blockElem = browser.find_element_by_xpath("//select[@name='BlockCode']")
block_select = Select(blockElem)
block = block_select.select_by_visible_text('All Blocks')
#select the year
yearElem = browser.find_element_by_xpath("//select[@name='Year']")
year_select = Select(yearElem)
year = year_select.select_by_visible_text('2016-2017')
#select the batch
batchElem = browser.find_element_by_xpath("//select[@name='Batch']")
batch_select = Select(batchElem)
batch = batch_select.select_by_visible_text('All Batches')
#select the funding agency
collabElem =
browser.find_element_by_xpath("//select[@name='FundingAgency']")
collab_select = Select(collabElem)
collab = collab_select.select_by_visible_text('Regular PMGSY')
# check the roadwise box
checkElem = browser.find_element_by_xpath("//input[@name='RoadWise']")
browser.execute_script("arguments[0].click();", checkElem)
# click on the view button
viewElem = browser.find_element_by_xpath("//input[@type='button']")
viewElem.click()
#switch to a new frame
browser.switch_to_frame(browser.find_element_by_xpath("//iframe"))
WebDriverWait(browser, 40).until(
EC.element_to_be_clickable((By.XPATH,"//table[@title='Export drop down
menu']")))
saveElem = browser.find_element_by_xpath("//table[@title='Export drop down
menu']")
saveElem.click()
#excelElem = browser.find_element_by_xpath("//a[@title='Excel']")
#excelElem.click()
#browser.execute_script("arguments[0].click();", excelElem)
代码成功运行,但是没有单击“保存”按钮。令人惊讶的是,一旦我在我的spyder编辑器中运行代码。然后在IPython shell中键入saveElem.click(),单击该按钮。
我太初学了解正在发生的事情。
答案 0 :(得分:0)
这是工作代码:
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.support import expected_conditions as EC
import time
browser = webdriver.Chrome()
browser.implicitly_wait(10)
browser.get("http://omms.nic.in")
progElem = browser.find_element_by_link_text("Progress Monitoring").click()
summElem = browser.find_element_by_link_text("Physical and Financial Project Summary").click()
# Select the state.
stateElem = browser.find_element_by_xpath("//select[@name='StateCode']")
state_select = Select(stateElem).select_by_visible_text("Andhra Pradesh")
# Select the district.
distElem = browser.find_element_by_xpath("//select[@name='DistrictCode']")
dist_select = Select(distElem).select_by_visible_text("All Districts")
# Select the block.
blockElem = browser.find_element_by_xpath("//select[@name='BlockCode']")
block_select = Select(blockElem).select_by_visible_text("All Blocks")
# Select the year.
yearElem = browser.find_element_by_xpath("//select[@name='Year']")
year_select = Select(yearElem).select_by_visible_text("2016-2017")
# Select the batch.
batchElem = browser.find_element_by_xpath("//select[@name='Batch']")
batch_select = Select(batchElem).select_by_visible_text("All Batches")
# Select the funding agency.
collabElem = browser.find_element_by_xpath("//select[@name='FundingAgency']")
collab_select = Select(collabElem).select_by_visible_text("Regular PMGSY")
# Check the road wise box.
checkElem = browser.find_element_by_xpath("//input[@name='RoadWise']")
browser.execute_script("arguments[0].click();", checkElem)
# Click on the view button.
browser.find_element_by_xpath("//input[@type='button']").click()
time.sleep(5)
# Switch to a new frame.
browser.switch_to.frame(browser.find_element_by_xpath("//iframe"))
# Click on the "Excel" button.
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Export drop down menu']"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div/a[@title='Excel']"))).click()
# Switch back to the main content.
browser.switch_to.default_content()
time.sleep(5)
browser.quit()