Search code examples
selenium-webdriverweb-scrapingbuttonclick

Clicking on a button | Selenium


Website: https://eljur.ru/login. This is a site where I want to enter data and parse the login page. But there is a problem: I know how to click on the buttons, but here at the beginning there is a field "Выберите образовательное учреждение"("Choose an educational institution") where you need to click and a search will appear, where you also need to find your school. Please help me make it so that I can click on "Выберите образовательное учреждение"("Choose an educational institution") and go to the school search.[enter image description here](https://i.sstatic.net/G0yyK.jpg)

I tried to find element with class "form-select-button--icon-left" and also "form-select-button_placeholder" and click but nothing worked.


Solution

  • You may try this code. Since you seem to be a new contributor, am sending you the complete class.

    package basics;
    
    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 java.time.Duration;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.function.Function;
    
    import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfElementsToBeMoreThan;
    import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
    
    public class Eljur {
        private static WebDriver driver;
        private static final Duration TIMEOUT_DURATION = Duration.ofSeconds(30);
    
        public static void main(String[] args) {
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://eljur.ru/login");
            customWait(1);
            findElement(By.cssSelector("div[role='combobox']")).click();
    
            findElement(By.cssSelector("input.form-input__input")).sendKeys("Y");
            findElements(By.cssSelector("div.form-select-option")).get(0).click();
            driver.quit();
        }
    
        public static void customWait(int seconds) {
            try {
                Thread.sleep(seconds * 1000L);
            } catch (Exception ignored) {
            }
        }
    
        public static WebElement findElement(By by) {
                return waitFor(visibilityOfElementLocated(by));
        }
    
        public static List<WebElement> findElements(By by) {
                return waitFor(numberOfElementsToBeMoreThan(by,0));
        }
    
        public static<T> T waitFor(Function<WebDriver,T> function) {
                return new WebDriverWait(driver, TIMEOUT_DURATION)
                        .until(function);
        }
    }