【问题标题】:Need to click on a button in iFrame page需要点击 iFrame 页面中的一个按钮
【发布时间】:2015-08-20 02:44:39
【问题描述】:

我正在尝试点击“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();
            }
        }
    }
}

【问题讨论】:

  • 我没有看到您尝试切换到 IFRAME 的任何代码。那个代码在哪里?你谷歌“selenium C# switch to iframe”并尝试过什么吗?您尝试了什么,结果如何?
  • 我试过 Selenium。几次,几种方式。我对 selenium 的最大问题是它想要打开一个浏览器并且不能在 webBrowser 控件的范围内工作。如果我能让它始终如一地工作,我对此很好。我可以使用 NUnit 让它工作,但我必须打开一个 HTTP 服务器。
  • 我很抱歉没有发送我所有失败尝试的副本。我没有保留这些信息的习惯。

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


【解决方案1】:

你也可以试试这个:-

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

【讨论】:

    【解决方案2】:

    试试这个。

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

    【讨论】:

    • 或者一行:driver.SwitchTo().DefaultContent().SwitchTo().Frame("");
    • 所以,我的问题变成了......我是否应该停止尝试在 webBrowser 控件中执行此操作而只打开浏览器?
    • 是的。我认为使用一个 webDriver 来完成你需要的所有工作在逻辑上会更好..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多