因此,我需要在我们办公室的各种显示器上显示一堆Power BI报告。我本以为使用Edge会是最好的选择,因为它与MS都是MS产品,因此应该最适合Power BI使用。
我为此使用了以下Powershell脚本:
# Open an Edge window
start microsoft-edge:
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Microsoft Edge')
while(1 -eq 1){
$wshell=New-Object -ComObject wscript.shell;
$wshell.AppActivate('Microsoft Edge'); # Activate on Edge browser
Sleep 2
$wshell.SendKeys('{F11}') #Open Edge in Fullscreen
Sleep 5
$fs = $edge.Document.DocumentElement.getElementsByClassName('glyphicon glyph-small pbi-glyph-fullscreen') | Select-Object -first 1
$fs.click()
Sleep 20; # Interval (in seconds) between switch
$wshell.SendKeys('^{PGDN}'); # Ctrl + Page Up keyboard shortcut to switch tab
Sleep 1; # Interval (in seconds) between refresh
$wshell.SendKeys('{F5}'); # F5 to refresh active page
}
该脚本旨在打开Edge(然后将打开我需要的默认启动页面),将Edge设置为全屏模式,单击Power BI全屏按钮,等待20秒,旋转选项卡,刷新选项卡。
我唯一无法工作的是从该网站的全屏按钮中单击Power BI全屏模式。我不断遇到错误:
You cannot call a method on a null-valued expression.
At line:12 char:5
+ $fs = $edge.Document.DocumentElement.getElementsByClassName('glyp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:13 char:5
+ $fs.click()
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
有人有什么想法吗? 干杯
答案 0 :(得分:0)
据我所知,Microsoft Edge将不支持COM自动化接口,但是Internet Explorer支持它。因此,我们无法使用Powershell从Microsoft Edge中找到网页元素。
因此,作为一种解决方法,您可以使用IE浏览器打开网站并找到打开PowerBI报告的按钮。
代码如下:
//Open IE Browser
$IE=new-object -com internetexplorer.application
//navigate the website.
$IE.navigate2("website url")
$IE.visible=$true
//Open IE in Fullscreen
$IE.FullScreen = $true
// find the hyperlink
$fs = $IE.document.getElementsByClassName('glyphicon glyph-small pbi-glyph-fullscreen') | Select-Object -First 1
//click the button to open the powerbi report
$fs.click()
对于Microsoft Edge浏览器自动化方案,我们可以使用Microsoft Edge WebDriver来实现。您可以参考以下C#示例:
安装Edge WebDriver后,可以打开Edge浏览器,并使用FindElementById或FindElementsByClassName方法查找按钮,然后调用元素click()方法打开报告。
/*
* This assumes you have added MicrosoftWebDriver.exe to your System Path.
* For help on adding an exe to your System Path, please see:
* https://msdn.microsoft.com/en-us/library/office/ee537574(v=office.14).aspx
*/
static void Main(string[] args)
{
/* You can find the latest version of Microsoft WebDriver here:
* https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
*/
var driver = new EdgeDriver();
// Navigate to Bing
driver.Url = "https://www.bing.com/";
// Find the search box and query for webdriver
var element = driver.FindElementById("sb_form_q");
element.SendKeys("webdriver");
element.SendKeys(Keys.Enter);
Console.ReadLine();
driver.Quit();
}
有关Microsoft Edge WebDriver的更多详细信息,请查看以下文章:
Getting started with WebDriver for Microsoft Edge
Bringing automated testing to Microsoft Edge through WebDriver