【问题标题】:WebDriver element found, but click returns nothing找到 WebDriver 元素,但单击不返回任何内容
【发布时间】: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


【解决方案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()

【讨论】:

  • 感谢拉特米尔的回复。不幸的是,同样的事情也会发生。代码在没有点击保存按钮的情况下成功运行。
  • 感谢 Ratmir,效果很好。如果你不介意回答,我有几个问题。 1)是 time.sleep(5) 让它工作。如果是,WebDriverWait 不应该处理加载延迟吗? 2)在您的原始评论中,您还建议更改“导出下拉菜单”按钮的 XPATH。我原来的 XPATH 错了吗?我怎么会知道或者是通过反复试验?非常感谢您回答我的扩展问题。最好的!!!
  • @Yogesh,如果我的回答有帮助 - 请在我的回答附近打勾。
  • 1) 是的,第一个time.sleep(5) 很有帮助,但是您可以使用WebDriverWait 来显示框架的外观。 2) 是的,您的 XPath 是错误的。您需要点击链接。希望对你有帮助!
猜你喜欢
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 2019-10-06
  • 2014-10-29
  • 2016-02-23
  • 1970-01-01
相关资源
最近更新 更多