How to Automate using selenium Java if data is there in divs and changes every time



  • enter image description here

    just as in the above image there is use of divs and every time the data (image) in that div changes. You are not even sure that how many divs will be therewith respect to row and columns. Kindly guide if any one has idea about it. Thanks.



  • I am not sure I understand completly so here are two solutions:

    If the image path does not change use css selector:

    driver.findElement(By.cssSelector("div[background-image$='medium-chicken-fajita-recipe\");.jpg']"))
    

    If the path does change and all you really need to do is select "some" product:

    Query all divs that have a class 'card-item-img' and select any of them:

    driver.findElements(By.className("card-item-img").get(1).click()
    

    Personally if that is the case I would use a lamba:

    driver.findElements(By.className("card-item-img")).stream().findFirst().orElseThrow(NotFoundException::new).click();
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2