开发者

Selenium Click method not working on an Xpath locator

开发者 https://www.devze.com 2023-01-21 12:20 出处:网络
Folks, My first time on Stack Overflow. Hope y\'all can help ... I\'m trying to use S开发者_JAVA百科elenium to click on the \'Buy Now\' button on an html page. Here\'s the HTML snippet:

Folks,

My first time on Stack Overflow. Hope y'all can help ...

I'm trying to use S开发者_JAVA百科elenium to click on the 'Buy Now' button on an html page. Here's the HTML snippet:

</div>
    <a href="javascript:void(0)" class="buttonCheckout">Buy Now</a>
    <a href="#" class="buttonSoldout">Sold Out</a>
</div>

I used the Selenium IDE to get the correct Xpath locator but the click event always fails.

>> chk = "id('yui-gen2')/x:div[3]/x:div/x:a[1]"
=> "id('yui-gen2')/x:div[3]/x:div/x:a[1]"
>> @selenium.get_xpath_count(chk)
=> "1"

>> @selenium.click(chk)
**Error**

>> chk = "xpath = " + chk
=> "xpath = id('yui-gen2')/x:div[3]/x:div/x:a[1]"
>> @selenium.click(chk)
**Error**

The **Error** is:
selenium-client received failure from selenium server:
requested:
 cmd=click
 1=xpath = id('yui-gen2')/x:div[3]/x:div/x:a[1]
 sessionId=d4d6796b3c9749139621cd0cbcde80b2
received:
 "ERROR: Element xpath = id('yui-gen2')/x:div[3]/x:div/x:a[1] not found"
 called from C:/Ruby187/lib/ruby/1.8/timeout.rb:67:in `timeout'
 enium::CommandError: ERROR: Element xpath = id('yui-gen2')/x:div[3]/x:div/x:a[1] not  found

I've tried some of the other suggestions on the forum - @selenium.double_click(chk), @selenium.fire_event(chk, 'click') - but those give the same Element not found error.

I've also tried all kinds of permutations of the above, tried others Xpaths (chk = "//a[@class='buttonCheckout']", chk = "/descendant::a[@class='buttonCheckout']", chk = "//a[@href='javascript:void(0)']".

In all of these cases, I believe I have found the right locator since the get_xpath_count method works but click always fails.

Any suggestions here? Thanks in advance!


Error message "Element xpath = ... not found" clearly states the real problem - so double click and fire event won't help.

You may try to install Selenium IDE plugin of Firefox, start recording and click on "Buy Now" button. Selenium IDE will automatically find some way to locate element. Also, it usually suggests several locator variants in drop-down list.

Also, you may try to use XPather / XPath checker plugins to find and check suitable xpath locators variants.


This is embarrassing but I figured it out ... the XPath locator was fine.

The issue was with the spaces in the statement:

chk = "xpath = " + chk

It should be:

chk = "xpath=" + chk

And then everything works nicely.

Newbies hah.

Thanks all.

0

精彩评论

暂无评论...
验证码 换一张
取 消