【问题标题】:Selenium does not execute click commandSelenium 不执行点击命令
【发布时间】:2017-05-23 03:24:46
【问题描述】:

我有一个 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;
            }
        }
    }
}

【问题讨论】:

    标签: c# selenium selenium-ide


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 同样的问题,我用的是IDE
      猜你喜欢
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2023-04-09
      • 2019-12-13
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      相关资源
      最近更新 更多