【发布时间】: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