Selenium webdriver脚本即使使用ID也无法找到按钮

时间:2012-11-05 11:17:08

标签: c# selenium automation webdriver selenium-webdriver

我希望这比看起来容易一些,我只是试图修复雪盲目!您的帮助将受到广泛赞赏。

我使用PageObject模型(我认为!)来创建我的测试。我有一个复制浏览和上传图像的对象,我不得不调用AutoIt脚本来完成。这样做但上传了图像,即使它具有ID属性,也没有找到“上传”按钮。

在文件选择后正确启用它,因此不确定我缺少什么。

PageObject的代码是:

public void AddImage(string FileDescription, int DocNo, int Revision, string StatusLstItem, string CreationDate)
{

    driver.FindElement(By.Id("Button1")).Click();
    driver.SwitchTo().Window("FileDetails");
    driver.FindElement(By.Id("Description1")).Clear();
    driver.FindElement(By.Id("Description1")).SendKeys(FileDescription);
    driver.FindElement(By.Id("DocNo1")).Clear();
    driver.FindElement(By.Id("DocNo1")).SendKeys(DocNo.ToString());
    driver.FindElement(By.Id("revision1")).Clear();
    driver.FindElement(By.Id("revision1")).SendKeys(Revision.ToString());
    new SelectElement(driver.FindElement(By.Id("FileStatus1"))).SelectByText(StatusLstItem);
    driver.FindElement(By.Id("creationDate1")).Clear();
    driver.FindElement(By.Id("creationDate1")).SendKeys(CreationDate);
    IWebElement element = driver.FindElement(By.Id("Filename1"));
    element.Click();
    Process.Start("C:\\Users\\Tom\\Desktop\\FileUploadCr.exe");
    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
    IWebElement upload = driver.FindElement(By.Id("action1"));
    upload.Click();
}  

然后我使用以下方法从测试类调用'AddImage'方法:

[Test]
public void ItemChecks()
{
    InformationObject informationObject = new InformationObject(driver);

    informationObject.ExpandAllButton();
    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
    informationObject.AddImage("SampleDesc", 01, 01, "Approved", "01/10/2012");
    informationObject.ClickUpdateButtonOne();
}

由于某种原因,它没有找到/点击'action1'上传按钮。我已经尝试切换回'FileDetails'窗口,但这也不起作用。

任何想法我都缺少什么?

仅供参考 - 上传按钮的代码为:

<input type="Submit" value="Upload" id="action1" disabled=""/>

UPDATE - 修改后的+工作PageObject方法现在看起来像这样:

public void AddImage(string FileDescription, int DocNo, int Revision, string StatusLstItem, string CreationDate)
{
    driver.FindElement(By.Id("Button1")).Click();
    driver.SwitchTo().Window("FileDetails");
    IWebElement element = driver.FindElement(By.Id("Filename1"));
    element.Click();
    Process.Start("C:\\Users\\Tom\\Documents\\Visual Studio 2010\\Projects\\AutoIt_Files\\FileUploadCr.exe");
    Thread.Sleep(1000);
    driver.FindElement(By.Id("Description1")).Clear();
    driver.FindElement(By.Id("Description1")).SendKeys(FileDescription);
    driver.FindElement(By.Id("DocNo1")).Clear();
    driver.FindElement(By.Id("DocNo1")).SendKeys(DocNo.ToString());
    driver.FindElement(By.Id("revision1")).Clear();
    driver.FindElement(By.Id("revision1")).SendKeys(Revision.ToString());
    new SelectElement(driver.FindElement(By.Id("FileStatus1"))).SelectByText(StatusLstItem);
    driver.FindElement(By.Id("creationDate1")).Clear();
    driver.FindElement(By.Id("creationDate1")).SendKeys(CreationDate);
    driver.SwitchTo().Window("FileDetails");
    Thread.Sleep(1000);
    IWebElement upload = driver.FindElement(By.Id("action1"));
    upload.Click();
    Thread.Sleep(1000);
    driver.SwitchTo().Alert().Dismiss(); //Ignore this, just an alert that displays following upload and not part of the solution
}   

2 个答案:

答案 0 :(得分:1)

尝试使用

driver.FindElement(By.id("action1")).submit();

答案 1 :(得分:0)

我们在这里遇到的问题是Selenium Web Driver无法找到&#34;上传按钮&#34;。

您使用的方法是FindBy。同上

我也面临同样的问题。

尝试使用不同的标识符。 对我来说CSSSelector Works。

例如:driver.FindElement(By.CssSelector(&#34; CssSelectorName&#34;))。点击();

注意:您可以使用firepath / firebug获取Xpath,CSS Selector以获得更多便利。