0

Using C# and Selenium webdriver for chrome, I trying to get

  1. Each Property, Setting and Config which is in Property tag.

  2. Their sub-item which is in sub-item.

  3. Their value which is in value.

i. No delimiter is used, thus I cant split text if any value has space in between. ii. Property, Setting, and Config are fixed; bu number of sub-item and their values are not fixed. So their positions keep on changing.

What approach should I take?

A sample HTML is given below.

<span class="list_props">
    <div style="height:8px;overflow:hidden;clear:left;"></div>
        <strong>Profile</strong>
            <strong style="font-style:italic;">Prop1:</strong>
                <a href="" title="P1-Profile Item 1">P1 Profile Item 1</a> 
            <strong style="font-style:italic;">Prop2:</strong> 
                <a href="" title="P2-Profile Item 1">P2 Profile Item 1</a> 
            <strong style="font-style:italic;">Prop3:</strong> 
                 <a href="" title="P3-Profile Item 1">P3 Profile Item 1</a>
                 <a href="" title="P3-Profile Item 2">P3 Profile Item 2</a>
    <div style="height:8px;overflow:hidden;clear:left;"></div>
        <strong>Settings</strong>
            <strong style="font-style:italic;">Setting1:</strong>
                <a href="" title="Setting1 Value1">Setting1 Value1</a>
    <div style="height:8px;overflow:hidden;clear:left;"></div>
        <strong>Config</strong>
            <strong style="font-style:italic;">Config:</strong>
                <a href="" title="config-1">config 1</a>
                <a href="" title="config-2">config 2</a> 
 </span>

I've tried a few things, but best I could reach is given below. Its not giving proper result.

var props = c.FindElement(By.XPath("//*[@id='ajax-prop-details-" + objectid + "']"))
.FindElements(By.XPath(".//span[@class='list_props']/")).ToList();

foreach(var ca in props)
    {
        Console.WriteLine("> " + ca.Text);
        var attribs = ca.FindElements(By.XPath(".//following-sibling::")).ToList();
            foreach (var atrb in attribs)
                {
                Console.WriteLine(">> " + atrb.Text);
                }
    }
Hesoti
  • 87
  • 1
  • 8

1 Answers1

1

Your XPaths are slightly off,

  • .//span[@class='list_props']/ should be changed to .//span[@class='list_props']
  • .//following-sibling:: should be changed to .//following-sibling::*

What I would suggest is to test out your XPaths in Chrome's inspect element's find section.

enter image description here

Given updates above, your code will be functional as demonstrated below:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Linq;

namespace FindingElements
{
    public class ProgramB
    {
        public static IWebDriver Driver = new ChromeDriver();

        private static void Test()
        {
            var props = Driver.FindElements(By.XPath("//span[@class='list_props']")).ToList();

            foreach (var ca in props)
            {
                Console.WriteLine("> " + ca.Text);
                var attribs = ca.FindElements(By.XPath(".//following-sibling::*")).ToList();
                foreach (var atrb in attribs)
                {
                    Console.WriteLine(">> " + atrb.Text);
                }
            }
        }
        public static void Main(string[] args)
        {
            Driver.Navigate().GoToUrl("file:///D:/temp/example.html");

            Test();
        }
    }
}

Saying this, it is still difficult to achieve what you intend in the first place, reason being that the property tag is not an actual html parent of its the sub-items and its values, rather a "logical" parent. This makes it difficult yet still possible to locate using XPath which is commonly used for locating elements using their parent/child relations and not elements which precede or succeed another element. I would suggest that you also take a look here which can help in achieving this type of element locating.

Reza Biglari
  • 64
  • 1
  • 1
  • 6