How to select today from html date picker instead of just putting the date there using selenuim and testng for a maven project

huangapple go评论113阅读模式
英文:

How to select today from html date picker instead of just putting the date there using selenuim and testng for a maven project

问题

我无法点击日历中的"今天"按钮。这是一个网站的示例:https://www.wufoo.com/html5/date-type/<input type="date">是我所说的日期的HTML代码。如何选择并点击日历中的"今天"按钮?

这是一个我想要点击"今天"按钮的示例图片:How to select today from html date picker instead of just putting the date there using selenuim and testng for a maven project

我可以发送日期值到日期输入字段,但我希望点击"今天"按钮而不是将日期作为输入发送。

这是pom.xml文件的内容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>delete_it</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>delete_it</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>

    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>31.0.1-jre</version>
    </dependency>

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.9.8</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
    <dependency>
      <groupId>io.github.bonigarcia</groupId>
      <artifactId>webdrivermanager</artifactId>
      <version>5.3.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.36</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
    <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
    </dependency>

    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-firefox-driver</artifactId>
      <version>4.1.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

这是测试文件的内容:

package org.example;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.time.LocalDate;

public class AppTest
{
    public static WebDriver driver;
    public LocalDate today = LocalDate.now();

    @Test
    public void wow() throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.wufoo.com/html5/date-type/");
        Thread.sleep(0500);
        driver.findElement(By.xpath("//*[@id=\"main\"]/section[1]/div/p[3]/input")).click(); // want to click the calender button. but it is clicking the text input field
    }
}
英文:

I can not click the today button in calender. here is an example in this website <https://www.wufoo.com/html5/date-type/>. &lt;input type=&quot;date&quot;&gt; this is the html for date i am talking about. how to select and click today button in this.

How to select today from html date picker instead of just putting the date there using selenuim and testng for a maven project.

I could send the date value in the date input field. But I am expecting to click the today button instead of sending the date as input

Here is the pom.xml file

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;org.example&lt;/groupId&gt;
&lt;artifactId&gt;delete_it&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;name&gt;delete_it&lt;/name&gt;
&lt;url&gt;http://maven.apache.org&lt;/url&gt;
&lt;properties&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
&lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
&lt;version&gt;3.141.59&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.guava&lt;/groupId&gt;
&lt;artifactId&gt;guava&lt;/artifactId&gt;
&lt;version&gt;31.0.1-jre&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.testng&lt;/groupId&gt;
&lt;artifactId&gt;testng&lt;/artifactId&gt;
&lt;version&gt;6.9.8&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager --&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.github.bonigarcia&lt;/groupId&gt;
&lt;artifactId&gt;webdrivermanager&lt;/artifactId&gt;
&lt;version&gt;5.3.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
&lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
&lt;version&gt;1.7.36&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.googlecode.json-simple&lt;/groupId&gt;
&lt;artifactId&gt;json-simple&lt;/artifactId&gt;
&lt;version&gt;1.1.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
&lt;artifactId&gt;selenium-firefox-driver&lt;/artifactId&gt;
&lt;version&gt;4.1.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;source&gt;1.8&lt;/source&gt;
&lt;target&gt;1.8&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

This is the test file

package org.example;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.time.LocalDate;
public class AppTest
{
public static WebDriver driver;
public LocalDate today = LocalDate.now();
@Test
public void wow() throws InterruptedException {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(&quot;https://www.wufoo.com/html5/date-type/&quot;);
Thread.sleep(0500);
driver.findElement(By.xpath(&quot;//*[@id=\&quot;main\&quot;]/section[1]/div/p[3]/input&quot;)).click(); // want to click the calender button. but it is clicking the text input field
}
}

答案1

得分: 1

请参考下面的工作代码及解释:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://www.wufoo.com/html5/date-type/");
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
    // 下面的代码将接受 cookies
    wait.until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
    
    // 下面的两行代码将以 dd/MM/yyyy 格式获取下一个星期一的日期
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDateTime now = LocalDateTime.now();
    String nextMonday = Objects.toString(dtf.format(now.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY)))));
    
    // 下面的代码将下一个星期一的日期发送到日期选择器的输入框中
    wait.until(ExpectedConditions.elementToBeClickable(By.name("date"))).sendKeys(nextMonday);
}

所需导入的包:

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Objects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

结果:

How to select today from html date picker instead of just putting the date there using selenuim and testng for a maven project

英文:

Refer the below working code with explanation:

public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(&quot;https://www.wufoo.com/html5/date-type/&quot;);
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
// below line will accept cookies
wait.until(ExpectedConditions.elementToBeClickable(By.id(&quot;onetrust-accept-btn-handler&quot;))).click();
// below 2 lines will get the next Monday in dd/MM/yyyy format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(&quot;dd/MM/yyyy&quot;);  
LocalDateTime now = LocalDateTime.now();
String nextMonday = Objects.toString(dtf.format(now.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY))));
// below line will send next Monday&#39;s date to date picker input text box
wait.until(ExpectedConditions.elementToBeClickable(By.name(&quot;date&quot;))).sendKeys(nextMonday);
}

Required imports:

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Objects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

Result:

How to select today from html date picker instead of just putting the date there using selenuim and testng for a maven project

答案2

得分: 1

这是要翻译的内容:

这并不容易做到,因为打开的日历实际上不是 DOM 元素。
所以,作为本地点击的用户行为,你只能通过坐标模拟点击(我建议你不要这样做)。

然而,在你的情况下,你可以通过 sendKeys 将日期直接传递给输入框,这将是最简单的解决方案。

WebElement dateElement = wdwait.until(ExpectedConditions.visibilityOfElementLocated(new By.ByCssSelector("[type=date]")));
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy");
dateElement.sendKeys(currentDate.format(formatter));
英文:

It's not easy to do, as far as opened calendar is actually not DOM element.
So 'user behaviour' as native click you can only simulate as clicking by coordinates (which I suggest you not to do).

However. you can pass date in input itself via sendKeys, that would be easiest solution in your case.

WebElement dateElement = wdwait.until(ExpectedConditions.visibilityOfElementLocated(new By.ByCssSelector(&quot;[type=date]&quot;)));
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;ddMMyyyy&quot;);
dateElement.sendKeys(currentDate.format(formatter));

huangapple
  • 本文由 发表于 2023年8月9日 12:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定