【问题标题】:Select today's date from a date picker - Selenium, C#, Visual Studio从日期选择器中选择今天的日期 - Selenium、C#、Visual Studio
【发布时间】:2017-10-06 11:29:14
【问题描述】:

我正在尝试编写一个自动化测试,要求浏览器从日期选择器中选择今天的日期,但我无法解决或找到有关如何执行此操作的任何信息。我只能找到信息要求它选择一个指定的日期。但我希望我的测试选择它自己运行时恰好是“今天”的任何日期。

到目前为止我的代码...

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using FluentAssertions;
using System;

namespace UnitTestProject5
{
[TestClass]
public class Portal
{
    [TestMethod]
    public void TestMethod1()
    {
        IWebDriver Driver = new ChromeDriver();

        // Logging in and other tests are here

        // Setting the COT Date (CotDate field location set earlier. The date picker appears when you click the CotDate field)
        CotDate.Click();
        var DatePicker = Driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_wizDetails_chk_cot"));

        //Close and Quit Chrome
        Driver.Close();
        Driver.Quit();

    }
}
}

如果答案在其他地方,我将非常感谢它的链接 :) 从我目前看到的情况来看,看起来我可能需要使用 JavaScript?

谢谢,

谢里登

【问题讨论】:

  • I can only find information to ask it to select a specified date. 这不是你想要做的吗?其中指定日期是DateTime.Today?
  • 你可以试试这个string date = DatePicker.GetAttribute("value"); 并告诉我
  • @mjwills 是的。不久前,我使用“DateTime.Today”尝试了一些操作,但不知道该怎么做。测试一直失败。
  • @NarendraR - 添加的测试通过,但没有点击今天的日期。
  • 你能告诉我们网址,以便我们帮助你

标签: c# visual-studio selenium datepicker automated-tests


【解决方案1】:

我不得不多次使用 FindElementByLinkText() 和日期选择器。获取您要查找的日期的 DateTime 对象并拆分出您需要的值(在本例中为 Day int 属性)并转换为字符串。在 FindElementByLinkText() 中使用该值作为参数。

var today = DateTime.Today;

var driver = new ChromeDriver();

driver.Navigate().GoToUrl("http://seleniumpractise.blogspot.in/2016/08/how-to-handle-calendar-in-selenium.html");

driver.FindElementById("datepicker").Click();

driver.FindElementByLinkText(today.Day.ToString()).Click();

driver.Quit();

【讨论】:

  • 感谢您的建议。到目前为止,我已经尝试过这一点以及其他建议的一些变化,但它没有发挥作用。调试时出现如下错误:
  • OpenQA.Selenium.NoSuchElementException 发生 HResult=0x80131500 消息=没有这样的元素:无法找到元素:{"method":"link text","selector":"09/10/2017 14: 28:12"}(会话信息:chrome=61.0.3163.100)(驱动程序信息:chromedriver=2.32.498550(9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),平台=Windows NT 10.0.15063 x86_64)源=WebDriver
  • 选择的日期将填充为 2017 年 10 月 9 日,而不是 2017 年 10 月 9 日或 2017 年 9 月 10 日,这会有所不同吗?
  • 您是传入整个 DateTime 对象,还是仅传递与链接文本匹配的对象片段?该错误消息听起来像是您正在传递完整的日期时间对象,并且没有日期选择器会在链接中包含时间......所以它找不到与参数匹配的链接。换句话说,如果用户是用户而不是 selenium,用户是否会单击一个表示日期的数字的链接?那么它只是一个你需要 selenium 传递给 FindByLinkText() 的数字,而不是完整的日期或 DateTime。
  • 空间不足...但目标是选择并单击与今天日期匹配的链接,这与匹配日期不同。您需要将 DateTime.Today 分解为当天数字日期的字符串,然后使用 THAT 查找链接文本。即,如果需要选择日期选择器链接为 9 日,则需要 FindByLinkText("9"),并从 DateTime.Today.Day 属性中获取 9。
【解决方案2】:

您可以使用此函数并将其传递给定位器

DateTime.Now.ToString("M/d/yyyy");

结果:“2017 年 10 月 7 日”

DateTime.Now.ToString("M-d-yyyy");

结果:“10-7-2017”

DateTime.Now.ToString("yyyy-MM-dd");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多