2

I am trying to create a script that will click same name button on several pages but the buttons are with different attributes. Some have id, some name, some value and some data as identifier. This function should have a list of attributes and when I run the script it should go into that list click the button and move to the next step. My problem here is when one of the buttons has 2 of the attributes in the list then it clicks twice. This is the problem I am trying to solve.

I tried try and catch function but and several others which I don't know exactly as I am a newbie to this. Try and catch function works for clicking but not to avoid clicking twice.

My try and catch function

    public void jeg_trykker_pƄ_knap(String knap) {
        switch (knap) {

case "Slet":
            try { 
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                driver.findElement(By.xpath("//*[@data-contribution-title='Din indberetning slettes']")).click();
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                } catch ( Exception a) {

                }
            try {    
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                driver.findElement(By.xpath("//*[@id='slet']")).click();
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                } catch ( Exception b) {

                }
            try {   
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                driver.findElement(By.xpath("//*[@value='Slet']")).click();
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                } catch ( Exception c) {

                }
            try {   
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                driver.findElement(By.xpath("//button[text()='Slet']")).click();
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                } catch ( Exception d) {

            try {   
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                driver.findElement(By.xpath("//*[@name='Slet']")).click();
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                } catch ( Exception e) {
                }   
                }           
            break;
rtgrt
  • 23
  • 4

1 Answers1

2

You can use OR condition of XPATH.

Example as below:

//*[@value='Slet'] | //*[@name='Slet']

In above example | denotes for OR

By this way you don't need to write different code and switch

Create your Xpath as per the example xpath I have shown

Source:

Using an OR condition in Xpath to identify the same element

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125