当前位置: 首页 > 面试题库 >

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

柳韬
2023-03-14
问题内容

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

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


问题答案:

这是工作代码:

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


 类似资料:
  • 我正在尝试用Selenium WebDriver、Java、Junit和Cucumber BDD来自动化一个航空公司的web应用程序。我能够创建一个简单的场景,比如选择一个带有出发日期的单程航班,然后选择一个座位,然后继续到付款页面。我能够写出所有的测试步骤,直到到达付款页面。Selenium成功地对所有页面中的所有元素执行了操作,但对支付页面中的任何元素执行了操作,却不成功。我尝试了隐式wait

  • 然而,我知道它是失败的前一个命令(如下所示),因为它从来没有点击到这个页面。 失败代码; 也试过;

  • 脚本: 登录www.flipkart。com,并在成功登录后从“电子产品”中选择“三星”。现在,我需要滚动到页面底部,从左侧,我需要点击可用性来选择“排除股票期权”,但点击可用性,我会收到消息 失败:Test_Samsung org。openqa。硒。WebDriverException:元素在点(119,9)处不可单击。其他元素将收到点击:

  • 问题内容: 我正在尝试编写Selenium测试用例,并从Firefox中的Selenium IDE开始。那里的测试工作很好。现在,我想使用Selenium Webdriver自动化该测试,并导出相应的JAVA类。到目前为止,一切都已设置并且运行良好(Internet Explorer窗口正在打开,显示相应的页面)。 但是:Selenium Webdriver找不到元素。我想获得以下元素: 而且我有

  • 一个测试用例在chrome驱动程序中随机失败 我正在检查Wait.Until(ExpectedConditions.ElementToBeclickable(ele));我也试过线程。睡眠

  • 我已经用selenium编写了一个自动化测试用例来测试登录页面,应该点击忘记密码链接。浏览器打开并转到给定的url,但忘记密码链接不会自动点击,有人能告诉我的代码有什么问题吗。 基本代码 Browsers.java 数据属性 控制台: 超文本标记语言代码