【问题标题】:Selenium(datepicker issue) [duplicate]Selenium(日期选择器问题)[重复]
【发布时间】:2019-07-23 05:55:26
【问题描述】:

我正在尝试自动化日期选择器,单击确定按钮时,日期应显示在日期框中。我有两个日期选择器,例如开始日期,另一个用于结束日期。一个日期选择器正在注册日期但是另一个日期选择器没有注册日期,我也没有收到任何错误。请帮忙,很紧急。这是我的代码..

package DatePicker;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DatePickerTest 
{
    WebDriver driver;
    Actions builder;
    @BeforeMethod
    public void setup()
    {
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.get("http://localhost/able/public/index.php/get_inputs");
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    System.out.println("Hello client");
    }

    @Test
    public void TestDate() throws Exception
    {
     WebDriverWait wait=new WebDriverWait(driver,3);
        Boolean invisible=wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("preloader")));
        if(invisible)
        {
            driver.findElement(By.xpath("//*[@id=\"add-edit-form\"]/div[3]/div[1]")).click();

        }
        Thread.sleep(2000);




        WebElement web11=driver.findElement(By.className("dtp-btn-ok"));
         web11.click();
         Thread.sleep(2000);


         driver.findElement(By.id("Contract_e_date")).click();
         Thread.sleep(1000);

         builder= new Actions(driver);
         //Thread.sleep(1000);
         WebElement web2=driver.findElement(By.className("dtp-btn-ok"));
         //Thread.sleep(1000);
         builder.moveToElement(web2).click(web2);
            //web2.click();
            Thread.sleep(1000);
         builder.perform();



    }


    }





【问题讨论】:

  • 用相关的 HTML 更新问题

标签: selenium datepicker


【解决方案1】:

看这里:

WebElement web11=driver.findElement(By.className("dtp-btn-ok"));
WebElement web2=driver.findElement(By.className("dtp-btn-ok"));

如果您的 2 个日期选择器具有相同的 CSS class - Selenium 将始终找到 第一个 并在找到的第一个项目上执行操作。

快速而肮脏的解决方案是切换到WebDriver.findElements() 函数,该函数返回ListWebElements,例如:

List<WebElement> datepickers = driver.findElements(By.className("dtp-btn-ok"));
WebElement web11 = datepickers.get(0);
WebElement web2 = datepickers.get(1);

更好的解决方案是选择正确的Locator Strategy,以便您的定位器唯一匹配这个或那个日期选择器(查看他们的父母、相关文本等)

还要注意using Thread.sleep is a some form of a performance anti-pattern,请考虑改用WebDriverWait,如果需要,请查看How to use Selenium to test web applications using AJAX technology 了解更多详情。

【讨论】:

  • 非常感谢您的解决方案,如果我在不同的页面中只使用两个日期选择器并按照上面给出的逻辑运行它可以工作,但在我的项目中所有其他元素都具有相同的代码它不起作用。
  • 之前给日期选择器的解决方案与我的日期选择器问题不同(我的问题被标记为重复)。正在使用的日期选择器有一个确定按钮。点击我只想注册日期。我的代码运行良好,我没有错误,没有异常,但单击确定按钮时没有显示日期。给我的解决方案很有用,但又面临同样的问题。谁能给我还有其他解决方案吗?非常感谢。
猜你喜欢
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多