需要单击iFrame页面中的按钮

时间:2015-08-20 02:44:40

标签: winforms visual-studio-2010 c#-4.0 selenium-webdriver

我想点击" btnPunch"。此按钮位于iFrame中。我无法完成这项工作。我已经使用Selenium IDE来记录这个被点击的按钮并创建了一个DLL,它也在NUnit中运行这个过程而没有任何问题。任何帮助将在三个月的工作后得到赞赏。 谢谢

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using NUnit.Framework;
using Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium;

namespace TimeClockEntry
{
    public partial class PNow : Form
    {
        IWebDriver driver = new InternetExplorerDriver();
        //driver = new InternetExplorerDriver();
        //private ISelenium selenium;
        //private StringBuilder verificationErrors;

        //public void SetupTest()
        //{
        //    selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://ew23.ultipro.com/");
        //    selenium.Start();
        //    verificationErrors = new StringBuilder();
        //}

        int linkcount = 0;
        string userName;
        string passWord;

        public PNow()
        {
            InitializeComponent();
            webBrowser1.Navigate("https://ew23.ultipro.com/login.aspx");
        }

        private void PNow_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;

            userName = Properties.Settings.Default.userName;
            passWord = Properties.Settings.Default.passWord;
        }

        private void PNow_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Hide();
            welcome f1 = new welcome();
            f1.ShowDialog();
            this.Close();
        }
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.Focus();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            linkcount++;

            if (linkcount == 1)
            {
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_UserName").SetAttribute("value", userName);
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_Password").SetAttribute("value", passWord);
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_LoginButton").InvokeMember("click");
            }

            if (linkcount == 2)
            {
                HtmlElement link = (from HtmlElement elem in webBrowser1.Document.GetElementsByTagName("a")
                                    where elem.InnerHtml == "Time Clock Entry"
                                    select elem).ElementAt(0);
                link.InvokeMember("Click");
            }

            if (linkcount == 3)
            {
//                driver.FindElement(By.Id("btnPunch")).Click();
                webBrowser1.Document.GetElementById("ctl00_Content_lnkLogout").InvokeMember("click");
                this.Close();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个。

if (linkcount == 3)
{
//get back to basic html source.
driver.SwitchTo().DefaultContent();
//switch to new frame
driver.SwitchTo().Frame("<FRAME NAME OR ID>");
driver.FindElement(By.Id("btnPunch")).Click();
}

答案 1 :(得分:0)

您也可以尝试: -

if (linkcount == 3)
            {
                WebElement frameSwitch = driver.findElement(By.xpath("Xpath of iframe")); //Frame Xpath
                driver.switchTo().frame(frameSwitch);
                driver.FindElement(By.Id("btnPunch")).Click();
                webBrowser1.Document.GetElementById("ctl00_Content_lnkLogout").InvokeMember("click");
                driver.switchTo().defaultContent();
                this.Close();
            }