Selenium不执行click命令

时间:2017-01-08 21:55:21

标签: c# selenium selenium-ide

我有一个id = submit的HTML按钮,所以使用Selenium IDE我选择了命令" Click"目标为id = submit但selenium IDE传递此行,就像它被执行但没有真正点击按钮所以表单没有提交!!怎么了?

这是我导出的C#代码。问题出在driver.FindElement(By.Name("submit")).Click();

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class Web
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "I cannot share url";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void TheWebTest()
        {
            driver.Navigate().GoToUrl("I cannot share url");
            driver.FindElement(By.Id("submit")).Click();
            for (int second = 0;; second++) {
                if (second >= 60) Assert.Fail("timeout");
                try
                {
                    if ("All Campaigns" == driver.FindElement(By.CssSelector("#CampaignsTable > div.box > div.title")).Text) break;
                }
                catch (Exception)
                {}
                Thread.Sleep(1000);
            }
            for (int second = 0;; second++) {
                if (second >= 60) Assert.Fail("timeout");
                try
                {
                    if ("Last" == driver.FindElement(By.Id("cloaker_last")).Text) break;
                }
                catch (Exception)
                {}
                Thread.Sleep(1000);
            }
            driver.FindElement(By.XPath("(//img[@alt='Click to edit the rotator settings'])[1]")).Click();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).Clear();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).SendKeys("dghfghf");
            driver.FindElement(By.Id("addNewUrl_0_geo_0_mobile_url")).Click();
            driver.FindElement(By.Id("submit")).Click();
            driver.FindElement(By.Id("submit")).Click();
            for (int second = 0;; second++) {
                if (second >= 60) Assert.Fail("timeout");
                try
                {
                    if ("Success" == driver.FindElement(By.CssSelector("div.message.green > span > b")).Text) break;
                }
                catch (Exception)
                {}
                Thread.Sleep(1000);
            }
            driver.FindElement(By.XPath("(//img[@alt='Click to edit the rotator settings'])[2]")).Click();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).Clear();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).SendKeys("tyujghghj");
            driver.FindElement(By.Name("submit")).Click();
            driver.FindElement(By.XPath("(//img[@alt='Click to edit the rotator settings'])[3]")).Click();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).Clear();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).SendKeys("gfhjghjgh");
            driver.FindElement(By.Name("submit")).Click();
            driver.FindElement(By.XPath("(//img[@alt='Click to edit the rotator settings'])[4]")).Click();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).Clear();
            driver.FindElement(By.Id("addNewUrl_0_geo_0_url_to_cloak")).SendKeys("ghghkghk");
            driver.FindElement(By.Id("addNewUrl_0_geo_0_mobile_url")).Click();
            driver.FindElement(By.Name("submit")).Click();
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText() {
            try {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert) {
                    alert.Accept();
                } else {
                    alert.Dismiss();
                }
                return alertText;
            } finally {
                acceptNextAlert = true;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

也许如果您共享现有代码,您尝试引用的网址等,我们可能会更有帮助。

如果没有足够的信息,很难回答问题。

答案 1 :(得分:0)

您还没有共享代码,因此很难分析您的问题。这可能对你有帮助 -

只需将您的命令click更改为clickAt,然后尝试。