Access and handle chrome extension popup using selenium webdriver

做~自己de王妃 提交于 2019-12-29 09:32:11

问题


I am trying to install the IBM DAP chrome extension using Selenium webdriver. I don't have access to crx file. So, I tried to install it directly through the chrome web store for plugins as:

browser = webdriver.Chrome()
browser.get('https://chrome.google.com/webstore/detail/dynamic-assessment-plugin/fnapgcgphlfhecijolobjodbbnjjpdga')
browser.maximize_window()
browser.implicitly_wait(5)
browser.find_element_by_css_selector("body > div.F-ia-k.S-ph.S-pb-qa > div.h-F-f-k.F-f-k > div > div > div.e-f-o > div.h-e-f-Ra-c.e-f-oh-Md-zb-k > div > div").click()
browser.switch_to_alert().accept()

but this code is not accessing the pop up window and is not able to click the 'Add extension' button. How to access the pop up window and click on the 'Add extension'?


回答1:


It is not the javascipt alert. It cannot be handled using Selenium Alert. It is a native os related window similar like file upload window.

In Java, we can use Robot class to simulate keyevent to handle this.

If it is windows os , we can use AutoIt script to handle the pop up. Please refer this related post for auto it usage. This is an exe and can be execute in any language.

I have tried in java using Robot class and it is working for me.

import io.github.bonigarcia.wdm.ChromeDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class InstallChromeExtention {
    public static void main(String[] args) throws IOException, AWTException, InterruptedException {
        ChromeDriverManager.getInstance().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://chrome.google.com/webstore/detail/google-keep-chrome-extens/lpcaedmchfhocbbapmcbpinfpgnhiddi");
        WebDriverWait wait = new WebDriverWait(driver, 60);
        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[aria-label='Add to Chrome']")));
        element.click();
        Thread.sleep(5000);
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_TAB);
        Thread.sleep(500);
        robot.keyPress(KeyEvent.VK_ENTER);
        Thread.sleep(500);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[aria-label='Remove from Chrome']")));
    }
}



回答2:


Following @Navarasu solution, I copied pyrobot.py file from here. It worked perfectly fine for me.

from pyrobot import Robot
from selenium import webdriver
import time

browser = webdriver.Chrome()
browser.get('https://chrome.google.com/webstore/detail/dynamic-assessment-plugin/fnapgcgphlfhecijolobjodbbnjjpdga')
browser.maximize_window()
browser.implicitly_wait(15)
browser.find_element_by_css_selector("[aria-label='Add to Chrome']").click()

time.sleep(3)
rob = Robot()
rob.key_press('tab')
rob.key_press('enter')


来源:https://stackoverflow.com/questions/52841197/access-and-handle-chrome-extension-popup-using-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!