0

I am working on rails functional testing automation with the help of capybara and selenium. We need to make use of xpath widely here and I am very new with xpath.

  1. Is the xpath used by cpaybara is different from the general xpath?
  2. How can we debug the error with xpath, is there any tool like firebug to make sure that the xpath is correct?( I know we can get the xpath from firebug using 'copy xpath' link, but that does not seems to be working).

Right now am stuck with the following error,

Failure/Error: find(:xpath, '//div[@class="comment"]/div/div/div/ul/li/a[@title="Delete"]').click

Any help is highly appreciated...

Edit: Edited the syntax error from the above code.

nkm
  • 5,844
  • 2
  • 24
  • 38

2 Answers2

2

You can try a contains function within the last anchor as an alternative

//div[@class="comment"]/div/div/div/ul/li/a[contains(text(), "Delete"]

To verify XPath checkout the XPath Visualizer

http://www.huttar.net/dimitre/XPV/TopXML-XPV.html

More details at this SO question

Community
  • 1
  • 1
carrutherji
  • 1,115
  • 10
  • 18
  • There is a missing close parenthesis in that xpath. Should be: `//div[@class="comment"]/div/div/div/ul/li/a[contains(text(), "Delete")]` – dbenton Dec 22 '17 at 18:59
0
  1. Is the xpath used by cpaybara is different from the general xpath ?

    Answer: xpath will be common but its not all time true because some framework doesn't support the xpath methods e.g.

    To locate a text using textnode i have below sample xpath

    //div[@id='some_id']/label/text()
    

    But selenium doesn't support to locate an element using textnode so above xpath won't work.

  2. How can we debug the error with xpath, is there any tool like firebug to make sure that the xpath is correct?

    Answer: In Chrome Browser v70 use below steps to evaluate and verify xpath:

    • Open chrome console by press F12 key
    • press Ctrl+F , it will open show textbox to find data, make sure Element tab selected in console
    • Now start writing you customized xpath here or you can paste the copied xpath here to check whether element is there for, it highlight the found element and also shows the count. Refer the below image:

enter image description here

  • To find auto generated xpath

    1. Right click on your intended element > inspect

      enter image description here

    2. It will highlight the corresponding element, select it and do right click. It show below option :

      enter image description here

    3. select the copy xpath, you will get the auto-generated xpath, you can evaluate the same as mention above to make sure its correct

NarendraR
  • 7,577
  • 10
  • 44
  • 82