Access and handle chrome extension popup using sel

2019-03-01 10:36发布

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'?

2条回答
劫难
2楼-- · 2019-03-01 11:20

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']")));
    }
}
查看更多
成全新的幸福
3楼-- · 2019-03-01 11:33

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')
查看更多
登录 后发表回答