selenium
  • selenium-webdriver
  • selenium-chromedriver
  • 2017-09-06 31 views 2 likes 
    2

    在我的項目中,有一個廣告提供商向我們顯示彈出廣告,但問題是這個彈出窗口會來它不是固定的。有時添加不顯示有時會彈出1分鐘後彈出並打斷我的測試用例。如何處理使用硒的外部彈出框

    我寫了一個關閉此彈出窗口的代碼,但它不是一個完美的解決方案,我認爲......如果任何人都可以提供幫助,

    boolean Imclose = wd.findElement(By.xpath(".//*[@class='IM_overlay_close_container IM_overlay_close_button']")).isDisplayed(); 
        if (Imclose == true) { 
         wd.findElement(By.xpath(".//*[@class='IM_overlay_close_container IM_overlay_close_button']")).click(); 
        } 
    
    +0

    使用警報界面 – iamsankalp89

    +0

    請問你能告訴我任何例子嗎? – zsbappa

    +0

    這個網站是公開的,所以我可以看看你正在處理什麼,它會更容易幫助你。我假設,彈出窗口可能不是一個警報,而是一個iframe /模式。 – Subh

    回答

    -1

    實際上它是在30-50秒後到來,所以你必須等待,然後點擊關閉按鈕。

    System.setProperty("webdriver.chrome.driver", "E:\\software and tools\\chromedriver_win32\\chromedriver.exe"); 
    WebDriver driver = new ChromeDriver(); 
    driver.get("https://www.rentbyowner.com/usa"); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); 
    WebElement ad=driver.findElement(By.xpath(".//*[@id='IM_target_overlay']/div/div/div/div[1]/a/div")); 
    WebDriverWait wait= new WebDriverWait(driver, 30); 
    wait.until(ExpectedConditions.visibilityOf(ad)); 
    ad.click(); 
    

    請參閱此鏈接瞭解更多信息:

    Handling Alerts in Selenium Webdriver

    +0

    我試過了你的代碼,但它不能關閉彈出頁面,我也提供了等待'180'秒,然後彈出頁面顯示,但是當我嘗試點擊關閉按鈕或文本,只需打開另一個瀏覽器窗口即可。 –

    +0

    請提供解決方案然後,因爲它在我的電腦上工作 – iamsankalp89

    0

    我認爲,這是在網頁中正常彈出覆蓋,而不是一個警告,它幾乎出現在60秒內。所以,下面的代碼可能適合你。

    //Waiting for the Popup to appear 
    WebDriverWait wait = new WebDriverWait(driver,60); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class,'IM_overlay_foreground')]"))); 
    
    //Clicking on the 'Close' text to close the popup. 
    driver.findElement(By.xpath("//div[contains(@class,'IM_overlay_foreground')]//span[@class='IM_close_text']")).click(); 
    

    注:可以增加時間從60到90,在情況下,它似乎需要更長的時間。事情是,只要彈出窗口出現在時間範圍內,它就被處理(如關閉)。

    +1

    我試過了你的代碼,但它不工作來關閉彈出頁面,我也提供了等待180秒,然後彈出頁面顯示,但是當我嘗試點擊關閉按鈕或文本,它只是打開另一個瀏覽器窗口。 –

    +0

    @Jainish,謝謝你的檢查和分析。當時我沒有資源看到這一點。:)當點擊'關閉'按鈕時,它實際上關閉了彈出窗口並同時打開一個新窗口。上面的代碼實際上和OP所要求的一樣。關閉新窗口只是一個選項。請在答案的評論部分找到進一步的說明。 – Subh

    1

    試試這些代碼,以處理不需要的彈出頁面。我提供了180秒的等待時間。然後點擊廣告彈出頁面內的關閉按鈕。

    點擊廣告彈出頁面後,另一個窗口打開,所以我必須重定向到我的主窗口,然後纔會執行進一步的代碼。

    driver.get("https://www.rentbyowner.com/usa"); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); 
    
        try 
        {   
          WebDriverWait wait = new WebDriverWait(driver, 180); 
          wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[contains(@class,'IM_overlay_foreground')]")))); 
          driver.findElement(By.xpath("//div[contains(@class,'IM_overlay_foreground')]//span[@class='IM_close_text']")).click(); 
    
    
          try 
          { 
           String winHandleBefore = driver.getWindowHandle(); 
           for(String winHandle : driver.getWindowHandles()) 
           { 
            driver.switchTo().window(winHandle); 
            //driver.switchTo().window(winHandle).close(); 
           } 
           driver.switchTo().window(winHandleBefore); 
    
           //Verify purpose written code, weather my driver is getting move to parent window or not. 
    
           WebDriverWait element = new WebDriverWait(driver, 10); 
           element.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("top_k_search")))); 
           driver.findElement(By.id("top_k_search")).clear(); 
           System.out.println("Try block"); 
          } 
          catch(Exception a) 
          { 
           System.out.println("Inner Catch"); 
          } 
         } 
         catch(Exception e) 
         { 
          System.out.println("Element not present"); 
          //Provide your code here.. 
         } 
    
    +0

    你有沒有試過這段代碼? –

    +0

    **單擊「關閉」按鈕時,新窗口的手柄不會切換到。重點是完全在父母/主窗口上**但是,如果你想關閉新的/子窗口,你將使用上面的代碼來切換和關閉它。但是,關閉彈出窗口的主要目的已經在外部'try-catch塊'的前3行代碼中處理了。 – Subh

    +0

    感謝您的評論Subh。但不知何故彈出頁面沒有關閉,所以我試試這個解決方案。 –

    相關問題