Home > database >  How to click on the button with Java Selenium
How to click on the button with Java Selenium

Time:07-23

I am trying to click the button first which I did and then the dropdown menu element which is Tüm Soru Tipleri automatically in Selenium Java.

This one is didn't work:

driver.findElement(By.id("select2-question_types-sq-result-xih0--1")).click();

Could you help?

HTML snapshot:

enter image description here

Element snapshot:

enter image description here

CodePudding user response:

Try adding a wait of 2 ms between button and drop-down.

CodePudding user response:

Once you click and expand the further to click() on the desired <li> element with text as Tüm Soru Tipleri you need to induce WebDriverWait for the elementToBeClickable() and you can use the following locator strategy:

  • Using xpath and text():

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='select2-results']/ul[@id='select2-question_types-sq-results']//li[text()='Tüm Soru Tipleri']"))).click();
    
  • Using xpath and contains():

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='select2-results']/ul[@id='select2-question_types-sq-results']//li[contains(., 'Tüm Soru Tipleri')]"))).click();
    
  • Related