改变eBay的网页内容/ DOM使用Selenium最简单的方法 [英] Easiest way to alter eBay page content/DOM using Selenium

查看:759
本文介绍了改变eBay的网页内容/ DOM使用Selenium最简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

硒显然不是为了让你修改浏览器页面的DOM,但我偶尔需要动态插入HTML元素

Selenium was apparently not designed to allow you modify the DOM of the browser pages, but I occasionally need to insert HTML elements dynamically.

在这种情况下:我使用Firefox与硒IDE记录在eBay上拍卖上市,但我发现它无法与这些元素重JScripted拍卖内容控制和记录没有处理。

In this instance: I am using Firefox with the Selenium IDE to record listing of auctions on eBay, but I noticed it cannot cope with the heavily JScripted auction content control and records nothing for those elements.

如果我关闭JavaScript在易趣网站上的第一次拍卖的登录页面的简单得多的版本被打破(它缺少确认贝宝作为付款方式,需要一个隐藏的输入元素)。

If I turn off JavaScript for the ebay site the much simpler version of the first auction entry page is broken (it is missing a hidden input element required to confirm PayPal as a payment option).

所以,我此刻的选项......一)弄清楚如何与硒看中JScript的HTML编辑器控制进行互动,或b)将新的元素融入到DOM。

So, my options at the moment are... a) figure out how to interact with the fancy JScript HTML editor control from Selenium, or b) insert new elements into the DOM.

没有人有任何建议,最好是在C#,因为我下一个Windows控制台应用程序自动化呢?的硒用户的扩展也将是可以接受的,如果任何人有代码。

Does anyone have any suggestions, preferably in C# as I am automating this under a Windows console application? A Selenium User-Extensions would also be acceptable if anyone has the code.

推荐答案

我已经找到一个解决方案,到目前为止,所以我想最好还是分享审核/改进。

I have found one solution so far so I thought it best to share it for review/improvement.

硒可以让您 。扩大与用户extensions.js文件行为

例如该硒内创建一个新的insertHtml命令:

For example this creates a new insertHtml command within Selenium:

Selenium.prototype.doInsertHtml = function(locator, text){
    var element = this.page().findElement(locator);
    var innerHTML = text + element.innerHTML;
    element.innerHTML = innerHTML;
}

有关硒IDE使用您只需包括延伸通过的选项菜单文件IDE本身。下次启动它会自动具有任何可用的新命令(从用户的扩展文件)的IDE。

For Selenium IDE usage you simply include the extensions file via the options menu of the IDE itself. Next time you start the IDE it automatically has any new commands available (from the user extensions file).

拓展硒

它可以是相当简单的延长硒,加入你自己的行为,断言和定位的策略。这是通过添加方法,以硒对象原型,并且PageBot对象原型与JavaScript完成。在启动时,硒会自动通过这些原型方法查找,使用名称的模式来识别哪些行动,断言和定位器。

It can be quite simple to extend Selenium, adding your own actions, assertions and locator-strategies. This is done with javascript by adding methods to the Selenium object prototype, and the PageBot object prototype. On startup, Selenium will automatically look through methods on these prototypes, using name patterns to recognise which ones are actions, assertions and locators.

下面的例子尽量给予指示如何硒可以用JavaScript进行扩展。

The following examples try to give an indication of how Selenium can be extended with javascript.

操作

在硒原型所有doFoo方法被添加为动作。对于每个操作符也有注册的动作fooAndWait。 。操作方法可能需要长达2个参数,这将是通过在测试第二和第三列的值

All doFoo methods on the Selenium prototype are added as actions. For each action foo there is also an action fooAndWait registered. An action method can take up to 2 parameters, which will be passed the second and third column values in the test.

例如:添加一个typeRepeated行动,硒,哪些类型的文本两次到一个文本框。

Example: Add a "typeRepeated" action to Selenium, which types the text twice into a text box.

Selenium.prototype.doTypeRepeated = function(locator, text) {
    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);

    // Create the text to type
    var valueToType = text + text;

    // Replace the element text with the new text
    this.page().replaceText(element, valueToType);
};

这篇关于改变eBay的网页内容/ DOM使用Selenium最简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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