【问题标题】:Perl, Selenium::Remote::Driver - using Xpath to select the 2nd element in an HTML form where both elements have identical 'value' entriesPerl, Selenium::Remote::Driver - 使用 Xpath 选择 HTML 表单中的第二个元素,其中两个元素具有相同的“值”条目
【发布时间】:2013-02-20 17:46:56
【问题描述】:

我试图弄清楚如何在 Seleniumn::Remote::Driver 的 Perl 模块中正确使用 XPATH。具体来说,我有一个 HTML 表单,其中包含各种输入字段,其中 2 个输入字段具有相同的值。我想根据它的“值”选择第二个输入字段(这个值在两个输入字段中是相同的)。我已经在 Selenium IDE(1.10.0) 中尝试过这个,它将目标显示为:

 xpath=(//input[@value='clickhere'])[2]

我正在尝试在使用 Selenium::Remote::Driver 模块的 Perl 脚本中模拟这种行为。

$elem = $driver->find_element("xpath=(//input[\@value='clickhere'][2])");
$driver->mouse_move_to_location(element => $elem, xoffset => 0, yoffset => 0);  
$driver->click;

这不起作用并引发错误(见下文)。我认为错误与“[2]”部分有关,因为我通常能够在 HTML 表单中选择具有唯一“值”的元素。将 xpath 编码到 find_element 函数中的正确方法是什么,以便它选择并单击 HTML 表单中的第二个元素,其中第一个和第二个元素的值都为“cickhere”?

Error while executing command: Argument was an invalid selector (e.g. XPath/CSS).: The given selector xpath=(//input[@value='click'][2]) is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression xpath=(//input[@value='clickhere'][2]) because of the following error:
[Exception... "The expression cannot be converted to return the specified type."  code: "0" nsresult: "0x805b0034 (TypeError)"  location: "file:///C:/Temp/anonymous6984397347674679222webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 5696"]
Command duration or timeout: 16 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.30.0', revision: 'dc1ef9c', time: '2013-02-19 00:15:27'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_11'
Session ID: ce542470-8a3a-41da-a73a-2a55275290d3
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=19.0}] at C:\Perl\scripts\frontend\createAccount.pl line 144.

【问题讨论】:

    标签: perl selenium selenium-rc


    【解决方案1】:

    docs 显示采用 XPath 的方法。

    定位器方案可以作为单独的arg传递,但这里不需要,因为默认是xpath

    试试

    my $elem = $driver->find_element('//input[@value="clickhere"][2]');
    

    【讨论】:

    • +1 正确的建议,几乎是正确的答案。 @CODEBLACK 的原始定位器是 xpath=(//input[@value='clickhere'])[2],所以答案是 ind_element('(//input[@value='clickhere'])[2]'); 在这种情况下括号会有所不同。
    • 谢谢各位!这是我使用的最后(工作)行: $elem = $driver->find_element("(//input[\@value='clickhere'])[2]");
    • //input[position()=2 和 @value='clickhere'] 是否适合您?这可能是另一种选择,虽然我还没有尝试过。
    • @Phil Perry,我发布的代码和 CODEBLACK 发布的代码与值为 clickhere 的第二个输入相匹配。如果其值为clickhere,则您的代码与第二个输入相匹配。目前尚不清楚 CODEBLACK 是什么意思,但正如他所说,他的代码有效......
    • @ikegami 嗨,我需要一些支持。 stackoverflow.com/questions/62755049/…
    猜你喜欢
    • 2022-12-16
    • 2017-09-22
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多