0

I am trying to figure out how to use XPATH correctly in the Perl module of Seleniumn::Remote::Driver. Specifically, I have an HTML form that contains various input fields where 2 of the input fields have identical values. I want to select the 2nd input field based on its 'value'(this value is identical in both input fields). I've tried this in the Selenium IDE(1.10.0) which displays the target as:

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

I'm trying to emulate this behavior in a Perl script that uses the Selenium::Remote::Driver module.

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

This does not work and an error is thrown(see below). I assume the error has to do with the '[2]' part as I am normally able to select elements that have a unique 'value' within an HTML form. What is the correct way to code the xpath into the find_element function so that it selects and clicks the 2nd element in an HTML form where both the 1st and 2nd element have the value '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.
CODEBLACK
  • 1,239
  • 1
  • 15
  • 26

1 Answers1

4

The docs show the method taking an XPath.

The locator scheme can be passed as a separate arg, but there's no need here because the default is xpath.

Try

my $elem = $driver->find_element('//input[@value="clickhere"][2]');
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    +1 Right advice, almost right answer. @CODEBLACK's original locator was `xpath=(//input[@value='clickhere'])[2]`, so the answer is `ind_element('(//input[@value='clickhere'])[2]');` The parentheses make a difference in this case. – Ross Patterson Feb 20 '13 at 11:58
  • thx guys! here is the final (working) line i used: $elem = $driver->find_element("(//input[\@value='clickhere'])[2]"); – CODEBLACK Feb 20 '13 at 12:39
  • Does //input[position()=2 and @value='clickhere'] work for you? That might be an alternative, though I haven't tried it. – Phil Perry Jun 07 '13 at 17:31
  • @Phil Perry, The code I posted and the code CODEBLACK posted matches the second input with value `clickhere`. Your code matches the second input if its value is `clickhere`. It's unclear what CODEBLACK meant, but seeing as he said his code works... – ikegami Jun 07 '13 at 17:57
  • @ikegami Hi, I need some support . https://stackoverflow.com/questions/62755049/cannot-fetch-value-from-table-using-seleniumremotedriver-in-perl – Sahal Jul 06 '20 at 12:39