0

Same button is used for activate and deactivate. First time when the code run button clicked and "Deactivated" successfully.But on second time the code can't able to find the element. First case button is in second 'a' tag and then deactivated the button it is in first 'a' tag.

Case 1:

<td>
  <a class="btn btn-success btn-sm fa fa-edit" href="/ClassDetails/Edit?ClassID=CLSS1012201800050">Edit </a>
  <a class="btn btn-danger btn-sm fa fa-trash-o" href="/ClassDetails/Deactivate?ClassID=CLSS1012201800050">Deactivate</a>
</td>

Case 2:

<td>
  <a class="btn btn-info btn-sm  fa fa-check-square-o" href="/ClassDetails/Activate?ClassID=CLSS1012201800050">Activate</a>
</td>   

Xpath is

WebElement deactivatebutton = driver.findElement(By.xpath("//*[@id='tblClassName']/tbody/tr[2]/td[4]/a[2]"));
//WebElement activatebutton = driver.findElement(By.xpath("//*[@id='tblClassName']/tbody/tr[2]/td[4]/a"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

6 Answers6

2

You can use two approaches here:

  1. You can use two xpaths, one each for Activate and Deactivate button using their text like:

    WebElement activateButton = driver.findElement(By.xpath("//a[text()='Activate']"));
    WebElement deactivateButton = driver.findElement(By.xpath("//a[text()='Deactivate']"));

  2. Make it parameterised by making a click method and sending the text of your button to it, this will be more useful as you can use this method for any button present on your webpage(with the same type of xpath).

    WebElement element;
    public void clickElement(String elementText){
    element = driver.findElement(By.xpath("//a[text()="+elementText+"]"));
    element.click();
    }

And then call the method like clickElement("Activate"); or clickElement("Deactivate");

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
1

My solution will be to construct simple Locator Strategies based on the available attributes:

  • Click on Deactivate link:

    • cssSelector:

      driver.findElement(By.cssSelector("a.btn.btn-danger.btn-sm.fa.fa-trash-o[href^='/ClassDetails/Deactivate?ClassID=']")).click();
      
    • xpath:

      driver.findElement(By.xpath("//a[@class='btn btn-danger btn-sm fa fa-trash-o' and starts-with(@href, '/ClassDetails/Deactivate?ClassID=')][contains(., 'Deactivate')]")).click();
      
  • Click on Activate link:

    • cssSelector:

      driver.findElement(By.cssSelector("a.btn.btn-info.btn-sm.fa.fa-check-square-o[href^='/ClassDetails/Activate?ClassID=']")).click();
      
    • xpath:

      driver.findElement(By.xpath("//a[@class='btn btn-info btn-sm  fa fa-check-square-o' and starts-with(@href, '/ClassDetails/Activate?ClassID=')][contains(., 'Activate')]")).click();
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can execute the java-script click() on the disabled button as :

WebElement deactivatebutton = driver.findElement(By.xpath("//*[@id='tblClassName']/tbody/tr[2]/td[4]/a[2]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", deactivatebutton);
Vikas Suryawanshi
  • 522
  • 1
  • 5
  • 12
  • congratulations, you have just modified your test to always pass even if the button can never be enabled and an end user will never be able to use the web page in the expected way. I wish people would stop reaching for the JavascriptExecutor to frig their way around every single problem when 9 times out of 10 it's not appropriate – Ardesco Mar 04 '19 at 09:39
0

Use following cssSelectors instead of xpaths:

For Activate Btn:

a[href*='/ClassDetails/Activate']

For Deactivate Button:

a[href*='/ClassDetails/Deactivate']

Remember you need to check/handle whether Activate/Deactivate button is present or not to click on them.

Hope this helped.

anurag0510
  • 763
  • 1
  • 8
  • 17
0

Since it is a link, linktext locator can also be used

// to activate
driver.findElement(By.linkText("Activate")).click();
//to deactivate
driver.findElement(By.linkText("Deactivate")).click();
SKYLINE
  • 46
  • 4
0

Not cleared my issue. Please check the attachment.Going to click Deactivate button

After deactivated,again second run of code, going to click data activation. In this case one one button

I used to write the below code.That's works fine. But I know that is not a valid one. I need to Deactivate and Activate the same button in two different Runs.

try {

        boolean deactivatebutton = driver.findElement(By.xpath("//*[@id='tblClassName']/tbody/tr[2]/td[4]/a[2]")).isDisplayed();
         WebElement dbutton = driver.findElement(By.xpath("//*[@id='tblClassName']/tbody/tr[2]/td[4]/a[2]"));
            dbutton.click();
        }

     catch(Exception e) {
    boolean activatebuttonpresent =  driver.findElement(By.xpath("//*[@id='tblClassName']/tbody/tr[2]/td[4]/a")).isEnabled();

          driver.findElement(By.xpath("//*[@id='tblClassName']/tbody/tr[2]/td[4]/a")).click();