我想捕获活动浏览器标签页的网址吗? 我已经通过使用下面的代码在c#中使用UI自动化实现了它的正常工作,但是我想使用java实现相同的功能,以便可以支持Linux,mac环境。是否有Java中像c#这样的UIAutomation,如果可以的话,我们可以使用它来实现。任何建议都将受到欢迎。您是否在使用任何公用设备,我们可以为Mac做到这一点,我已获得此链接https://apple.stackexchange.com/questions/232565/get-url-of-opened-tab-tabs-from-terminal
///--------------------------------------------------------------------------------
/// <summary>
/// Get URL by using AutomatioElement.
/// </summary>
/// <param name="process"></param>
/// <param name="logPrefix"></param>
/// <returns>Url</returns>
private string GetProcessURL(Process process, string logPrefix)
{
AutomationElement UrlBarElement = null;
AutomationElement mainWindowElement = null;
AutomationElement rootElement = null;
try
{
mainWindowElement = AutomationElement.FromHandle(process.MainWindowHandle);
if (mainWindowElement == null)
{
log.Warn(logPrefix + " - Unable to capture the URL as MainWindowElement is Null.");
return null;
}
switch (process.ProcessName)
{
case ApplicationConstants.MicrosoftEdge:
rootElement = AutomationElement.FromHandle(GetDesktopWindow());
foreach (AutomationElement child in rootElement.FindAll(TreeScope.Children, PropertyCondition.TrueCondition))
{
AutomationElement window = GetEdgeCommandsWindow(child);
if (window == null) // not edge
continue;
return GetEdgeUrl(window);
}
break;
case ApplicationConstants.Opera:
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.OperaNameProperty));
break;
case ApplicationConstants.Chrome:
Condition chromeConditions = new OrCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.ChromeNameProperty));
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.ChromeNameProperty));
break;
case ApplicationConstants.FireFox:
Condition fireFoxConditions = new AndCondition(
new PropertyCondition(AutomationElement.NameProperty, ApplicationConstants.FireFoxNameProperty),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, fireFoxConditions);
if (UrlBarElement == null)
{
fireFoxConditions = new OrCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, fireFoxConditions);
}
break;
default:
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
}
//Comment Before Deployment.
//Thread.Sleep(5000);
if (UrlBarElement != null)
{
return ((ValuePattern)UrlBarElement.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
else
{
log.Info(logPrefix + " - Unable to capture the URL because unable to detect the URLBarelement");
return null;
}
}
catch (Exception exx)
{
}
finally
{
mainWindowElement = null;
process = null;
UrlBarElement = null;
rootElement = null;
//GC.Collect();
}
return null;
}