1

`

    WebDriver Driver = null;
     WebDriverWait wait = null;
     @BeforeTest
      public void beforeTest() {
         System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver.exe");
         ChromeOptions options = new ChromeOptions();
         options.setExperimentalOption("excludeSwitches",Arrays.asList("disable-popup-blocking"));
         options.addArguments("--disable-popup-blocking");
         
         Driver = new ChromeDriver(options);
         Driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         wait = new WebDriverWait(Driver, 25);
      }
    
    @Test(dataProvider = "dp")
  public void Test( String s) {
        String SearchWord = s;
        String url = "https://www.firstcry.com/";
        Driver.get(url);
        wait.until(ExpectedConditions.alertIsPresent());
        
        By popupdeny1 = By.xpath("//*[@id=\"deny\"]");
        By searchbox = By.id("search_box");
        By sortSelect = By.xpath("/html/body/div[5]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div");
        By descendingPrice = By.xpath("/html/body/div[5]/div[2]/div/div[2]/div[1]/div[2]/div[1]/ul/li[4]/a");
        
        if(Driver.findElement(popupdeny1).isDisplayed())
            Driver.findElement(popupdeny1).click();`

I am automating firstcry webpage for a study purpose. There are few popups in this url, which i need to handle before searching for a product and sort the items based on price

https://www.firstcry.com/

enter image description here

I even added Chromeoptions in browser setting, but still the popups are coming...here is the code part. I tried to click the deny button in my code, but ended with error saying cant find the element.

Any help is appreciated

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
JeyanthiRanjit
  • 161
  • 1
  • 12

1 Answers1

1

To click() the element with text as Allow within the url https://www.firstcry.com/, as the the desired element is within a <iframe> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for the desired elementToBeClickable.

  • You can use either of the following Locator Strategies:

    • Using cssSelector:

      driver.get("https://www.firstcry.com/");
      new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='webpush-onsite']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#allow"))).click();
      
    • Using xpath:

      driver.get("https://www.firstcry.com/");
      new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@title='webpush-onsite']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='allow']"))).click();
      
  • Browser Snapshot:

firstcry

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thank you so much.. it helps a lot..but now i am facing another issue after searching for a product in the search box and sorted the listing items by price low to high, for second product search, the sorting is not working...any help here.. i am taking the xpath of sort selector – JeyanthiRanjit Jul 27 '20 at 20:14
  • @JeyanthiRanjit Sounds to be a new question all together. Can you raise a new question with your new requirement please? – undetected Selenium Jul 27 '20 at 20:17
  • 1
    Sure.. Thanks much for your help..Yes I did accept your answer... – JeyanthiRanjit Jul 28 '20 at 23:05