如何使用Selenium ExecuteScript函数获取HashTable - c#

使用Selenium WebDriver,一次为C#中的标签获取所有html属性,我这样做

ReadOnlyCollection<object> HtmlAttributes = (ReadOnlyCollection<object>)((IJavaScriptExecutor)Driver).ExecuteScript("var s = []; var attrs = arguments[0].attributes; for (var l = 0; l < attrs.length; ++l) { var a = attrs[l]; s.push(a.name + ':' + a.value); } ; return s;", ele);

但是有了这段JavaScript代码,我得到了一个带有值的数组:

HtmlAttributes[index] = "HtmlAttribute:Value".

是否可以获取哈希表?例如:

HtmlAttributes[HtmlAttribute] = "Value"

参考方案

为什么下面的工作不起作用?

// Putting this all on one line would work just fine; I'm
// breaking it out here for readability.
string script = 
    @"var s = {};
      var attrs = arguments[0].attributes;
      for (var index = 0; index < attrs.length; ++index) {
        var a = attrs[index];
        s[a.name] = a.value;
      }
      return s;";

// Direct casting would work in a single line as well.
// Again, using the "as" operator and multiple lines for
// readability.
// ASSUMPTIONS: "driver" is a valid IWebDriver object, and
// "element" is a valid IWebElement object found using FindElement.
IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
Dictionary<string, object> attributes = executor.ExecuteScript(script, element) as Dictionary<string, object>;

现在,有两个警告。首先是ExecuteScript中的序列化器不能很好地递归过于复杂的对象。这意味着,如果属性具有对象作为其值,则可能无法达到您想要的效果。例如,我不会尝试从JavaScript序列化jQuery对象。另一个警告是返回类型为Dictionary<string, object>。如果要创建Hashtable或将值转换为字符串,则必须从JavaScript获取值后自己转换这些值。

Selenium:如何使RemoteDriver始终附加到当前的浏览器选项卡? - javascript

我正在开发一个Windows应用程序,该应用程序可以通过语音命令操纵浏览器。我想适当地处理用户添加一些标签并根据需要更改所选标签的情况。事实证明,RemoteDriver仅与一个选项卡一起使用,并且可以通过提供选项卡手柄将焦点切换到另一个选项卡。但是我不知道如何获取选定的选项卡句柄并始终检查选定的选项卡是否已更改,或者是否存在始终与选定的选项卡一起使用的方法…

如何使用Selenium WebDriver在Chrome浏览器中测试是否成功下载文件 - java

在我的测试场景中,我确实使用selenium Web驱动程序单击了网页中存在的链接,并且此单击操作将开始将文件下载到计算机中……在此流程中,它不会打开任何窗口,询问要在哪里保存文件并保存到默认的下载文件夹..所以很好...现在我的目标是测试/验证此下载过程是否发生,或者是否使用WebDriver Selenies命令本身...?因此,基本上,下载需要一种断言…

如何使用Selenium Python注销linkedin - python

我正在尝试使用以下代码从linkedin注销,但它给了我这个错误:AttributeError:'list'对象没有属性'click'登录成功,但是不执行注销代码并退出。 from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.w…

Python Selenium:单击下拉菜单中的选项时可以更改值吗? - javascript

我正在使用python硒进行一些搜索。在我查询的一个网页上,搜索对话框允许我通过下拉菜单指定是否要搜索所有部分或特定部分。要选择哪个部分,该站点在弹出窗口中有一些单独的对话框,我可以单击一个部分,然后在内部为下拉菜单中的“此部分”选项分配选定部分的值。这是检查选择器时的外观: <select ...> <option id="se…

Selenium-python单击按钮始终返回错误 - javascript

我正在尝试使用python-selenium绑定单击一个按钮;到目前为止没有任何运气尝试过各种选择器。我正在使用Chromedriver。我可以选择使用elem = driver.find_element(by='xpath', value="//div[@id='gwt-debug-search-button…