一尘不染

找到了WebDriver元素,但单击不返回任何内容

selenium

我在现有帖子中找不到解决方案(尽管我一直在寻找)。在下拉菜单中进行选择后,我试图从代码中的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(),以单击该按钮。

我太初学者了,不明白发生了什么。


阅读 352

收藏
2020-06-26

共1个答案

一尘不染

这是工作代码:

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()
2020-06-26