Selenium - 转换的通用方式“WebElement”到Javascript或JQuery对象 [英] Selenium - Universal Way to Convert "WebElement" to Javascript or JQuery Object

查看:306
本文介绍了Selenium - 转换的通用方式“WebElement”到Javascript或JQuery对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WebElement,我想在我的对象上执行一系列(一个或多个)JS / JQuery类型的操作。

I have a a simple WebElement and I want to perform a series(one or more) of JS/JQuery type actions on my object.

大部分时间这很简单,我只需获取对象的ID,将其传递到ExecuteScript函数中,并且中提琴效果很好:

Most of the time this is easy, I simply get the ID of the object, pass that into the "ExecuteScript" function, and viola works great:

RemoteWebDriver driver = ...;
var element = driver.FindElementByXPath("..."); //Or any other type of lookup
driver.ExecuteScript(@"$('#" + element.GetAttribute("id") + "')."DoSomeStuff...;");

20%这根本不起作用,因为该项目没有ID。当然有很多种方法查找Selenium中的项目以及在jQuery或Javascript中查找项目的许多方法,但它们并不总是映射,虽然可以在jQuery中构造一个可以查找Selenium对象的查询,但该方法可能不相同每个对象类型。

20% this doesn't work at all because the item doesn't have an ID. Of course there are many ways to lookup items in Selenium and many ways to lookup items in jQuery or Javascript, but they don't always map, and while it may be possible to construct a query in jQuery that can lookup a Selenium object, that methodology cannot be the same for every object type.

搜索它似乎大多数人都使用id方法(例如: 1 2 3 )。你可以这样做对面方式。另一个想法是在jQuery中访问它之前为每个元素在selenium中提供一个唯一的ID,但它看起来不像可以工作因为要设置它你需要使用Javascript或jQuery。

Searching around it seems most people use the "id" method(Examples: 1, 2, 3). You can do this the opposite way. Another idea would be to give each element a unique ID in selenium before accessing it in jQuery, but it doesn't look like that would work because to set it you need to using Javascript or jQuery.

我还没有找到办法为每个元素执行通用,如何做到这一点?

I've yet to find a way to do this Universally for every element, how can this be done?

推荐答案

您可以并且始终能够通过 ExecuteScript 在JavaScript中来回传递元素引用。是否可以将这些原始DOM元素转换为JQuery使用,这是我没有资格发言的问题。然而,在JavaScript代码中引用WebDriver-found元素的代码将是这样的:

You can, and always have been able to, pass element references back and forth in JavaScript via ExecuteScript. Whether those raw DOM elements can be converted to be used by JQuery is a matter upon which I am not qualified to speak. Nevertheless, the code for referencing a WebDriver-found element in your JavaScript code would be something like this:

// Assume element holds a reference to an already-found
// IWebElement, found using the standard WebDriver FindElement
// methods, and that driver is a properly-instantiated
// IWebDriver object.
// N.B., doing proper casting here, since it's idiomatic in
// the WebDriver library to code to the interface, not the concrete
// implementation.
IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
executor.ExecuteScript("alert(arguments[0].tagName);", element);

上面的代码会抛出警报(并阻止你的Selenium代码),显示元素的 tagName JavaScript属性。您可以使用此技术来使用该元素,就像在页面中使用JavaScript一样。

The above code will throw up an alert (and block your Selenium code), showing the element's tagName JavaScript property. You can use this technique to use the element just as you would in JavaScript in the page.

这篇关于Selenium - 转换的通用方式“WebElement”到Javascript或JQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆