[英]How to open URL through default Chrome profile using Python Selenium Webdriver
[英]Open a chrome extension through Selenium WebDriver using Python
我创建了一个 chrome 扩展,它对数据库进行 API 调用并获取一些与当前打开的网站相关的数据。 例如,如果我打开 target.com 并单击扩展程序,它将为您提供与 target.com 相关的数据。
我正在尝试通过 selenium web 驱动程序为其编写自动化测试,我可以定期运行该驱动程序以进行回归测试。 要测试扩展程序,我需要先打开扩展程序(通常我们通过单击扩展程序图标来完成)。
我尝试了不同的方法来尝试点击扩展图标,但没有成功。 (例如,使用键盘快捷键 ALT - LEFT_ARROW - SPACE 但不能通过 webdriver 工作)。
我也试过这个(这里提到):
options = webdriver.ChromeOptions()
options.add_argument("--app-id = mbopgmdnpcbohhpnfglgohlbhfongabi")
但是上面的代码对打开扩展没有帮助。
我很感激有关如何在 Selenium Webdriver 中使用 python 执行此操作的任何想法。
我们有类似的需求,正在使用 Selenium WebDriver 开发 chrome 插件。 正如“@Aleksandar Popovic”所说,我们无法使用 WebDriver 单击 chrome 扩展图标,因为图标不在网页中。
我们使用sikuli (利用图像识别的自动化工具)来点击 chrome 插件。 在该附加弹出窗口之后将是另一个浏览器窗口,因此请使用切换窗口对附加弹出窗口执行操作。
这是同时使用Selenium Webdriver和Sikuli 的Java示例代码。
Sikuli 将基于图像识别运行。 在运行代码之前,Chrome 浏览器的屏幕截图并进行裁剪,以便图像中只有插件可用。 将该图像另存为“AddonIcon.png”。
Sikuli 将匹配屏幕上的图像(在我们的例子中为 AddonIcon.png )并模拟点击它的动作。
import java.io.File;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.sikuli.script.App;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
public class PageTest {
public static void main(String[] args) {
// Opening chrome with that addon
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("Path to ur chrome addon (.cxt file)"));
System.setProperty("webdriver.chrome.driver", "path to chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
// Creating object to the Sukali screen class
Screen s=new Screen();
//Finding and clicking on the Addon image
try {
s.find("Path to the 'AddonIcon.png'");
s.click("Path to the 'AddonIcon.png'");
} catch (FindFailed e) {
e.printStackTrace();
}
//Wait until new Addon popup is opened.
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
// Switch to the Addon Pop up
String parentWindow= driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for(String curWindow : allWindows){
if(!parentWindow.equals(curWindow)){
driver.switchTo().window(curWindow);
}
}
/***********Ur code to work on Add-on popup************************/
}
}
我希望这能帮到您。
Selenium 仅支持与 Web 视图交互 - 所以这是不可能的。
我一直在寻找这个问题的解决方案 - 结果没有。
有同样的问题。 通过使用链接解决它: chrome-extension://<the extension identity>/html/login.html
- 而不是图标。 这样,我可以测试扩展的所有功能。
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_extension("./testCRX/angular_ext.crx")
driver = webdriver.Chrome(chrome_options=option)
driver.get('chrome://extensions/')
打开添加的扩展:我想我们可以在 chrome://extensions/ -> 键盘快捷键中设置快捷键,将键盘快捷键添加到您添加的扩展中,然后发送键盘以启动扩展。
您可以使用此解决方案来模拟单击扩展图标
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.postMessage('clicked_browser_action', '*')");
我知道它不完全相同,但是您不能将 iframe 注入带有弹出窗口源的页面,然后与其交互以进行测试吗?
<iframe src='chrome-extension://<extensionId>/popup.html'/>
试试这个: 使用带有扩展名的 Python 运行 Selenium WebDriver(.crx 文件)
用
options.add_extension('path_to_extension.crx')
您可以通过路径指向解压扩展程序的选项将参数--load-extension
传递给 Chrome 网络驱动程序。 因此,在您的情况下,您可以使用: options.add_argument("--load-extension=ABSOLUTE_PATH_TO_EXTENSION")
此外, 这里是我编写的一些使用--load-extension
方法工作的代码的链接。 (它是 Ruby 而不是 Python,但应该会给你一些见解。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.