find element in selenium by css h2

1,628 views
Skip to first unread message

Maeva

unread,
Mar 31, 2015, 1:35:51 PM3/31/15
to webd...@googlegroups.com

I want to find the element of this link “Dropchord” in  <h2>

. I am trying this in craigslist. Any help will be highly appreciated


<a class="a-link-normal
s-access-detail-page
 a-text-normal"
title="Dropchord"
href="http://www.amazon.de/Double-Fine-Productions-Dropchord/dp/B00E0OK2X2/ref=sr_1_2?s=mobile-apps&amp;ie=UTF8&amp;qid=1427809716&amp;sr=1-2&amp;keywords=AS-Productions">

<h2 class="a-size-base a-color-null
s-inline s-access-title a-text-normal"
>

Dropchord

</h2></a>



thanks

Maeva

unread,
Apr 1, 2015, 7:57:23 AM4/1/15
to webd...@googlegroups.com
Please can you help me please
Thanks

sunny sachdeva

unread,
Apr 1, 2015, 8:26:54 AM4/1/15
to webd...@googlegroups.com
Is this what you are looking for

"//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"

Thanks
Sunny

Maeva

unread,
Apr 1, 2015, 8:57:32 AM4/1/15
to webd...@googlegroups.com
Hi,
Its just not working. when i Write
try
{
IList<IWebElement> AppName = select.FindElements(By.CssSelector("//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"));
OR
IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"));

                                foreach (IWebElement Name in AppName)
                                {
                                    if ()
                                    {

                                    }
                                }
}
 catch
{
}
get the programmed line the cursor goes directly to the function catch
what can i do?
thanks

sunny sachdeva

unread,
Apr 2, 2015, 7:19:03 AM4/2/15
to webd...@googlegroups.com
CSSSelector works different from XPath and so are their syntax.

So the locator which I sent was of XPath and it would return you single element and not the collection.

You can find this element and then try to use click method and if you face any exception then please update this thread.

Thanks
Sunny

{
IList<IWebElement> AppName = select.FindElements(By.CssSelector("//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"));
OR
IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[text( )='Dropchord']"));

Maeva

unread,
Apr 2, 2015, 9:18:52 AM4/2/15
to webd...@googlegroups.com
Hi,
Solution is
IList<IWebElement> AppName = driver.FindElements(By.XPath("//h2[@ class='a-size-base a-color-null s-inline s-access-title a-text-normal']"));
     

thanks

darrell

unread,
Apr 3, 2015, 12:05:49 PM4/3/15
to webd...@googlegroups.com
One word of warning. Using XPath to find an element by the class attribute when there are multiple words in the attribute is brittle. If the class order changes it will make ZERO difference to the website but it will cause your automation to fail. You should really use CSS selectors. I'm not on a Windows machine. So I cannot be sure of the syntax is correct but it would be something like:

     driver.FindElements(By.CSSSelector("h2.a-size-base.a-color-null.s-inline.s-access-title.a-text-normal"))

When using CSS the order of the class values does not matter. So if they change the order or add more words to the class attribute this should continue to work.
Reply all
Reply to author
Forward
0 new messages