Home > Enterprise >  How to find the exact element path without using xpath
How to find the exact element path without using xpath

Time:03-29

I'm currently trying to locate this check box. I know I can use a xpath to locate it but I'm trying to see if there's a more efficient way of doing it. The problem I'm seeing is that there are multiple div class with the same name. I'm trying to find this specific one and isolate it. I'm trying to make my code more efficient if possible.

Xpath

/html/body/div/div/div/div[1]/cow-data/cat-panel/section/div[1]/div/div/md-        checkbox[4]/div[1]

Element path:

<div  cd-gar-ripple="" cd-gar-ripple-checkbox=""><div ></div></div>

Code I'm trying to use:

find('cd-container').click

The problem I'm seeing is that the div id 'cd-container' has multiple occurrences on the page and thus this doesn't work. I'm trying to see if I can find a more efficient way of doing this.

CodePudding user response:

As per the HTML cd-container is the value of the class attribute but not id attribute. So your effective line of code will be:

find('.cd-container').click

CodePudding user response:

If you want to find an element (AND THEN), return it's xpath. Use capybara.

This will allow you to locate using text / css selector. And then you can just return the path of the element.

i.e.

page.find('td', text: 'Column 1').path # Random td with text
page.find('#main').path # ID
page.all('div').select { |element| element.text == 'COoL dIv' }.first.path # First div that matches certain text
page.find('.form > div:nth-of-type(2)').path # Specific structured div
page.all('p div li:nth-child(3)').sample.path # Random li
  • Related