【问题标题】:How to click on dynamic buttons when a certain WebElement condition is true当某个 WebElement 条件为真时如何单击动态按钮
【发布时间】:2020-04-20 09:58:44
【问题描述】:

以下是网页的 HTML 代码。

<div class="clickme" style="cursor:pointer; " id="qwer1234" title="" data-placement="bottom" data-toggle="tooltip" mode="">
        <div class="title">
            <a target="_blank" class="color" href="http://www.test.com">Test1</a> requested <a href="http://www.test.com">HolidayApproval</a></div>
            <div class="msg-time"><small><a rel="tooltip" data-placement="right" title="" class="site-color6" href="#" data-original-title=" 3:54:24 AM">1 day ago</a> 2020 to March 30, 2020 )</small></div>
            Auto Approval            <br>
            <div class="btn" id="qwert4567">
                <a id="HolidayAccept" class="button" href="javascript:void(0);">APPROVE</a><a id="qwer5678" class="test" href="javascript:void(0);">REJECT</a>                </div>    
            </div>

<div class="btn" id="qwer6789">
                <a id="HolidayAccept" class="test21" href="javascript:void(0);">APPROVE</a><a id="qwer7890" class="test" href="javascript:void(0);">REJECT</a>                </div>

我正在执行一个程序,条件是如果显示某个文本,那么我必须单击动态按钮。 如果以下条件为真

driver.findElements(By.xpath ("//div[starts-with(@id,'qwer')]//a[contains(text(),'Holiday')]"));

然后我会点击页面上的所有按钮

driver.findElement(By.xpath("//a[starts-with(@id,'HolidayReject')]")).click();

下面是这个程序。运行此程序后,它正在执行 else 块中的语句,而不是 if{} 块。有人可以告诉我如何点击 if{} 块内的拒绝按钮吗?

package Leaves;

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;

public class Leaves {

    public static void main(String[] args) {


        System.setProperty("webdriver.chrome.driver","F:\\Drivers\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("http//www.testing.com");

        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();

        driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

        driver.findElement(By.xpath("//input[@id = 'UserLogin_username']")).sendKeys("test");
        driver.findElement(By.xpath("//input[@id = 'UserLogin_password']")).sendKeys("test");
        driver.findElement(By.xpath("//button[@id = 'login-submit']")).click();

        driver.findElement(By.xpath("//df[@id = 'dashboard-header-open']")).click();
        driver.findElement(By.xpath("//img[@src = '/images/Icons_latest/pending-action.png']")).click();


        List <WebElement> linklist = driver.findElements(By.xpath
                ("//div[starts-with(@id,'5e9b7')]//a[contains(text(),'Holiday')]"));

        //List <WebElement> linklist = driver.findElements(By.tagName("div"));
        System.out.println(linklist.size());

        for(int i=0 ; i<linklist.size(); i++) {
            String linkText = linklist.get(i).getText();
            System.out.println(linkText);

            if(linkText == "Holiday") {
                driver.findElement(By.xpath("//a[starts-with(@id,'qwer')]")).click();
                System.out.println("Approvals rejected");
            }
            else {
                **strong text**System.out.println("Nothing to click");
            }
        }

        System.out.println("program reached end successfully");

        driver.quit();
        // TODO Auto-generated method stub

    }

}

【问题讨论】:

    标签: java eclipse selenium webdriver


    【解决方案1】:

    代替==,使用.equals比较String

    if(linkText.equals("Holiday")) {
        //
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2011-09-05
      相关资源
      最近更新 更多