Issue
Below is the HTML code for a webpage.
<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>
and
<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>
I am executing a program based on the condition that if a certain text is displayed then I have to click on the dynamic buttons. If the below condition is true
driver.findElements(By.xpath ("//div[starts-with(@id,'qwer')]//a[contains(text(),'Holiday')]"));
Then I will click on all the buttons on the page
driver.findElement(By.xpath("//a[starts-with(@id,'HolidayReject')]")).click();
Below is the program for this. After running this program it is executing the statements in the else block instead of if{} block. Can some one please let me know how to click on reject buttons which is inside if{} block?
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
}
}
Solution
Instead of ==
, to compare String
using .equals
:
if(linkText.equals("Holiday")) {
//
}
Answered By - frianH