gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:16:37 24 4
gpt4 key购买 nike

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

代码运行成功,但是没有单击保存按钮。令人惊讶的是,一旦我在我的 spy 编辑器中运行代码。然后在 IPython shell 中输入 saveElem.click(),按钮就会被单击。

我还是个初学者,无法理解发生了什么。

最佳答案

这是工作代码:

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

关于jquery - 找到 WebDriver 元素,但单击不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48602367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com