Home > Back-end >  SELENIUM web-driver -> cannot automate the click button in "li"
SELENIUM web-driver -> cannot automate the click button in "li"

Time:08-17

I am trying my selenium webdriver in java to click the 'Alerts' button on left at "https://demoqa.com/alertsWindows"

This is the element-code:

<li  id="item-1"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M145.2 96l66 746.6L512 928l299.6-85.4L878.9 96H145.2zm595 177.1l-4.8 47.2-1.7 19.5H382.3l8.2 94.2h335.1l-3.3 24.3-21.2 242.2-1.7 16.2-187 51.6v.3h-1.2l-.3.1v-.1h-.1l-188.6-52L310.8 572h91.1l6.5 73.2 102.4 27.7h.4l102-27.6 11.4-118.6H510.9v-.1H306l-22.8-253.5-1.7-24.3h460.3l-1.6 24.3z"></path></svg><span >Alerts</span></li>

At the moment i tried these options:

 List<WebElement>  Alert = driver.findElements(By.xpath("//li[@class='btn btn-light ' and @id='item-1']"));
              for(int i = 0; i< Alert.size() ; i   ) {
                if( i == 1) {
                    Alert.get(i).click();
                    break;
                }
              }

Please suggest a solution for this or alternative. I am a little novice. Thank you

CodePudding user response:

@SANAT, you can try the following code. Below does not include the code for opening the browser though. Basically, I selected a specific element instead of all three.

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
@Test
    public void alertInDeomQA() {
        driver.get("https://demoqa.com/alertsWindows");
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(7));
        WebElement alert = wait.until(ExpectedConditions
                .visibilityOfElementLocated(By.cssSelector(".element-group:nth-child(3) li[id='item-1']")));
        alert.click();
    }

CodePudding user response:

I see that an advertisement section at the footer is blocking the Alerts nav from being clicked. You can do two things here:

  1. Either get rid of the advertisement banner by clicking on x OR
  2. Scroll window to appropriate height that you would be able to click it.

I used the 2nd option;

driver.execute_script('window.scrollTo(0,500)')
driver.find_element(By.XPATH, "//*[text()='Alerts']//..").click()

Note: I write in python. I think most of it remains same in Java too.

  • Related