找到WebDriver元素,但单击不返回任何内容 [英] WebDriver element found, but click returns nothing

查看:72
本文介绍了找到WebDriver元素,但单击不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在现有帖子中找不到解决方案(虽然我一直在寻找)。我在下拉菜单中进行选择后尝试从代码中的URL中抓取数据。最后,我想点击保存按钮并下载excel文件。这里的代码工作正常,但最终没有点击保存按钮。

I couldn't find a solution in existing posts (though I have been looking). I am trying to scrape data from the URL in the code after making selections in the drop-down menu. In the end, I would like to click on the save button and download the excel file. Here is the code which works fine, but does not end up clicking on the save button.

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)

点击菜单中的进度监控项,然后点击物理和财务项目摘要项。然后我为每个下拉项目做出选择。

This clicks on the 'Progress Monitoring' item in the menu and then clicks on 'Physical and Financial Project Summary' item. Then I make a selection for each drop-down item.

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()。

The code runs successfully, however, does not click on the save button. Surprisingly, once I run the code in my spyder editor. And then type saveElem.click() in the IPython shell the button gets clicked.

我太初学了解了发生了什么。

I am too much a beginner to understand what is going on.

推荐答案

以下是工作代码:

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()

这篇关于找到WebDriver元素,但单击不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆