【发布时间】:2018-08-05 18:42:56
【问题描述】:
我在现有帖子中找不到解决方案(尽管我一直在寻找)。在下拉菜单中进行选择后,我试图从代码中的 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() 按钮被点击。
我太初学者了,无法理解发生了什么。
【问题讨论】:
-
嗨!进展如何?
-
@Ratmir。首先感谢您之前回答我的问题。我正在尝试为“状态”的不同选择运行一个循环。但是,在循环中,有时“Road Wise”框仍未选中。我尝试了各种方法(例如 time.sleep(10) 和 WebDriverWait),但无济于事。除了循环和其他一些小改动之外,我正在使用上面的代码。你有什么想法吗?我可以提交新代码,但不想替换原始代码。
-
这应该是一个单独的问题。
-
谢谢@RatmirAsanov。我在这里问了一个新问题:stackoverflow.com/questions/48814291/…
标签: jquery python-3.x selenium