【问题标题】:How to Click The Chrome Extension in C# Using Selenium?如何使用 Selenium 在 C# 中单击 Chrome 扩展?
【发布时间】:2021-05-05 02:43:24
【问题描述】:

我能够通过 CRX 文件加载名为 Desktopify 的 chrome 扩展,并使用带有 Chrome 选项的 C# 成功地将其添加到带有 selenium webdriver 的 chrome。我有 2 个问题。

  1. 谁能告诉我如何在添加到 chrome 后自动单击扩展程序?每次我都必须手动单击扩展程序以进行进一步的自动化过程。

  2. 扩展加载到ChromeDriver后,如何与扩展中的元素进行交互?

这是我迄今为止为问题 #1 所做的尝试......

ChromeOptions options = new ChromeOptions();
options.AddExtension(@"D:\Downloads\Desktopify\nlhjgcligpbnjphflfdbmabbmjidnmek.crx");
options.AddArgument("test-type");
System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"D:\VisualStudioExpress2017\Projects\MyApp\bin\Debug\chromedriver.exe");
driver = new ChromeDriver(options);

【问题讨论】:

标签: c# selenium automation selenium-chromedriver


【解决方案1】:

要点击扩展图标,您可以使用:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.postMessage('clicked_browser_action', '*')");

为了使用扩展元素,你应该切换到框架(你可以在 DOM 上看到,扩展是一个带有一些 id 的框架)

driver.SwitchTo().Frame(driver.FindElement(By.id("your_frame_Id")));

【讨论】:

  • 感谢 Norayr Sargsyan... 我尝试了该代码,但标题为空且未单击。看到这个链接...ibb.co/njxHGhm
  • 感谢@NorayrSargsyan8 ...我尝试了该代码,但标题为空且未单击。看到这个链接...ibb.co/njxHGhm
  • 尝试不分配给字符串
  • 谢谢@NorayrSargsyan ...我尝试了该代码,但标题为空并且没有点击。看到这个链接...ibb.co/njxHGhm
  • 谢谢@NorayrSargsyan。我试过这个js.ExecuteScript("window.postMessage('clicked_browser_action', '*')"); 还是不行
猜你喜欢
  • 1970-01-01
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 2016-07-22
  • 2016-03-17
  • 2022-11-20
相关资源
最近更新 更多