简体   繁体   English

如何在Java中的Selenium Webdriver中的Webelements中单击特定元素

[英]How to click on specific element out of the webelements in selenium webdriver in java

If the date from the list of dates is matching with the given date which is string format then it should click on that element from that list of dates which when fetched is converted to string only. 如果日期列表中的日期与给定的日期(字符串格式)匹配,则应单击该日期列表中的该元素,将其提取后仅转换为字符串。 I have list of dates like below when converted to string: 当转换为字符串时,我具有如下所示的日期列表:

2017-10-13  
2017-09-29  
2017-09-22  
2017-09-15  
2017-09-08  
2017-09-01  
2017-08-25  
2017-08-18 

So now if the input which is "2017-Sep-29" which when parsed to another format gives 2017-09-29 matches with the second date from the list then it should be clicked but it is throwing an error. 因此,现在如果输入为"2017-Sep-29" (当解析为另一种格式时得出的结果为2017-09-29与列表中的第二个日期匹配,则应单击该输入,但会引发错误。

Below is my java code with comments : 以下是我的带注释的Java代码:

WebDriver driver;
List<WebElement> stageListOfFilters;
String finalSnapshot = "", finalSnapshot1 = "";

@Test
public void getDateAndclick()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    try {
        Date varDate = dateFormat.parse("18-Aug-2017");
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        finalSnapshot1 = dateFormat.format(varDate).toString();
        System.out.println("Date: " + finalSnapshot1); // Prints 2017-08-18
        driver.findElement(By.xpath(config.getsnapshotFilter())).click();
        Thread.sleep(2000);
        finalSnap = finalSnapshot1.substring(5, 10);
        System.out.println("Date for final Comparison: " + finalSnap); // Prints 08-18 since i want to click on 18th august date
        stageListOfFilters = driver.findElements(By.xpath(".//*[@class='QvFrame DS']")); // This collects the list
                                                                                            // of dates which is
                                                                                            // fetched later on in
                                                                                            // for loop
        Thread.sleep(2000);
        for (int i = 0; i < stageListOfFilters.size(); i++) {
            System.out.println(stageListOfFilters.get(i).getText());
            Thread.sleep(2000);
            if (finalSnap.contains(stageListOfFilters.get(i).getText())) {
                driver.findElement(By.xpath(".//*[@class='QvFrame DS']/div/div/div[1]/div[2]/div[" + i + "]")) 
                        .click();// If it matches at that index then it should click on element present at that index  
                Thread.sleep(2000);
            }
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

So now it should click on the last element since date which is being passed is 18th August but it still clicks on 2017-09-29 . 因此,现在应该单击自日期之后的最后一个元素,即传递的日期是18th August但仍单击2017-09-29 I figured out that I am not getting value using get(i).getText() in if loop which is under for loop . 我发现我没有在for loop下的if loop使用get(i).getText()获得价值。 Why is it so ? 为什么会这样呢?
Code for getAttribute("title") data : getAttribute(“ title”)数据的代码:

stageListOfFilters = driver.findElements(By.xpath(".//*[@id='DS']")); // This collects the list
                                                                                    // of dates which is
                                                                                    // fetched later on in
                                                                                    // for loop

            for (WebElement element : stageListOfFilters) {
                System.out.println("Dates are: " + element.getAttribute("title"));
            }

Try to access the web elements in loop like show below: 尝试如下所示循环访问Web元素:

stageListOfFilters = driver.findElements(By.xpath(".//*[@class='QvFrame DS']")); 
Thread.sleep(2000);
for ( WebElement we: stageListOfFilters ) { 
    System.out.println(stageListOfFilters.getText());
    if (finalSnapshot1.contains(we.getText())) {
        we.click(); 
        System.out.println("Data" + we.getText());
        Thread.sleep(2000);
    }
}  

If you are fetching the date value correctly using the elements then I believe the problem could be in your "if" condition. 如果您使用元素正确地获取了日期值,那么我相信问题可能出在您的"if"条件下。 I believe the finalSnap value is "08-18" and whereas "stageListOfFilters.get(i).getText()" is fetching the date as "2017-08-18" . 我相信finalSnap值为"08-18" ,而"stageListOfFilters.get(i).getText()" "2017-08-18"日期提取为"2017-08-18" The below "If" condition inside the for loop will be always false since you are checking if "08-18" contains "2017-08-18" or not. for循环内的以下“ If”条件将始终为false,因为您正在检查"08-18"包含"2017-08-18"

if (finalSnap.contains(stageListOfFilters.get(i).getText())) { //this condition check is false

     //ur code
}

You should be comparing "2017-08-18" with "08-18" and not the reverse way. 您应该将"2017-08-18""08-18"进行比较,而不是相反。

I was not able to select the proper xpath of the date list. 我无法选择日期列表的正确xpath。 I solved it now and below is my working code which fulfills my expectations. 我现在解决了,下面是可以满足我期望的工作代码。 For css you can check this link: 对于css,您可以检查以下链接:

try {
            Date varDate = dateFormat.parse("01-Sep-2017");
            dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 2017-08-18
            finalSnapshot1 = dateFormat.format(varDate).toString();
            System.out.println("Date: " + finalSnapshot1); // Prints 2017-08-18
            driver.findElement(By.xpath(config.getsnapshotFilter())).click();
            Thread.sleep(2000);

            stageListOfFilters = driver
                    .findElements(By.xpath("(//*[@class='QvFrame DS'])[1]/div/div/div[1]/child::div")); // This collects
                                                                                                        // the list
            // of dates which is
            // fetched later on in
            // for loop
            System.out.println("Size" + stageListOfFilters.size());
            Thread.sleep(2000);
            for (int i = 1; i < stageListOfFilters.size(); i++) {

                if (finalSnapshot1.equals(stageListOfFilters.get(i).getText())) {
                    System.out.println("Filters list value: " + stageListOfFilters.get(i).getText());
                    System.out.println("Value to be compared: " + finalSnapshot1);
                    i = i + 1;
                    driver.findElement(By.xpath(".//*[@class='QvFrame DS']/div/div/div[1]/div[" + i + "]/div[1]"))
                            .click();
                    System.out.println("Value of i: " + i +" and Date is: "+ finalSnapshot1.toString());
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM