Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

No such element exception in Selenium code using Java

While i tried automating a website using selenium and Java i got no such element exception even though i got correct xpath while inspecting the field. After giving a city by sendkeys i tried to select 1st option from dropdown by select method and actions keyword but both didnt work and throwed exception. I will attach the code and image of inspected website.Kindly help with the issue

@Test
  public void abhibusHotel() throws Exception {
      driver.get("https://www.abhibus.com/");
      HomePage ohome=new HomePage(driver);
      ohome.hotelPage();
      HotelBooking obook=new HotelBooking(driver);
      obook.bookingDetails();
      Thread.sleep(2000);

public void bookingDetails()
    {
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(city)).sendKeys("Bengaluru");
            //Select se=new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//span[text()='Beng'])[1]/parent::div"))));
            //Select se=new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Beng'][1]"))));

            //se.selectByIndex(1);
            WebElement tooltip= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Beng']/parent::div")));
            oaction.moveToElement(tooltip).click().build().perform();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
    }

enter image description here

question from:https://stackoverflow.com/questions/66067923/no-such-element-exception-in-selenium-code-using-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your screenshot is saying that there are 9 matching elements. Selenium takes the first one. That first one is not necessarily visible. You need to narrow your xpath query so that it returns the only one that represents what is currently visible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...