Locate an HTML element searching by Title (selenium c#) - c#

How to locate an element by using search by title method, if that is the best way of searching for it in this specific case shown bellow?
HTML CODE:
<a data-toggle="modal" href="#login-window" class="login-window" data-placement="bottom" data-delay="500" title="" data-original-title="Launch the login window">
<div class="arrow-down"></div>
Login
</a>
I am trying to locate the element from above but there is also another element with almost the same properties apart from the Title. What is the best way of locating this element?

"Okay can you give me an code example for (in parent list which contains id 'userMenu' find anchor that contains partial href login-window)?"
according to your comments, by 'parent list' you meant <li> and by 'anchor' you meant <a>, so this is the xpath for that :
//li[contains(#id, 'userMenu')]/a[contains(#href, 'login-window')]

Related

Clicking on Href Button selenium

I have delt with an Href button in the past and didn't find it too hard, but this button is being a pain. I have tried clicking by xpath, class, and link text. None have worked. I know there are plenty of the same question out there, but most of them give answer's that I am already trying. Below is the code I have. The one thing I haven't tried is javascriptexecutor. I also have been clicking on it in the command line of chrome and it does work. Just can't get selenium too. It throws an element not found. Also I feel it is worthy to note that I did not find any IFrames that I need to switch to. The only things that concern me which maybe I do not have the knowledge of selenium to deal with is the HTML mentions header, main, section, div, ul, li , and a which I have seen all before except for ul and li. Thank you for any help someone provides.
wait.Until(ExpectedConditions.ElementToBeClickable(By.ClassName("card-header-link float-md-right"))).Click();
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[1]/main/section[1]/div/ul/li[5]/a"))).Click(); //full xpath
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[#id='app']/main/section[1]/div/ul/li[5]/a"))).Click();
wait.Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Security"))).Click();
HTML
<li data-v-91f16f3e="">
<a data-v-91f16f3e="" href="/security" class="">
<span data-v-91f16f3e="" class="icon icon-shield"></span>
<span data-v-91f16f3e="" class="text">Security</span></a>
</li>
try this xpath :
//span[text()='Security']/..
or
//span[text()='Security']/parent::a[#href='/security']
in code :
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[text()='Security']/.."))).Click();
but it is strange to know that By.LinkText("Security") did not work.
Update 1 :
try this css selector
div[class$='desktop'] li a[href$='security']
code :
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div[class$='desktop'] li a[href$='security']"))).Click();
I see the href attribute value is lowercased security, not Security.
So, please try this:
wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpath("//a[contains(#href,'security')]"))).Click();

Selenium C#: Get Element by title

I want to get an element by its title, I don't know if we have a universal solution for this in Selenium C#.
Here is the example of the HTML
<div class="student">
<a href="abcxyz.com">
<span class="title"> ZZZ</span>
</a>
</div>
And another example
<div class="page">
</div>
Of course if just one page, I will do it like
FindElement(By.ClassName("page")).GetAttribute("href");
or
FindElement(By.ClassName("student")).GetAttribute("href")
to get the "href" out of where the "title" is located. But they have pages with different designs, and the only common thing between them is that title. I wonder if there is any solution for me to find the "href" by the "title=ZZZ"?
Thanks in advance.
The two examples are different, I would use a union (|) operator of XPath expressions that will work in both cases. If you have more cases, you can add additional union operator, followed by the new case XPath:
//a[#title='ZZZ']/#href | //*[#class='title'][contains(text(),'ZZZ')]/parent::a/#href
Without searching for specific text:
//a[#title]/#href | //*[#class='title']/parent::a/#href
Try this xpath:
/a[#title="ZZZ"]/#href

Selenium find elements within specified element

I'm running into trouble with Selenium when it comes to XING's groups. The solution I was using for more than two years to this very date does not work anymore; they must have changed something about their HTML.
The main problem is that a certain select element doesn't open (when trying to open it by clicking it) and I hence cannot access the options to "click" or to select. I see a small border embracing the drop down, as if it was clicked but didn't function in second place.
Now that I've spent quite some time on elaborating on the error, I'm quite sure this is because the same (meaning the same attributes, ...) drop down exists on the same page at another point (to sort/filter posts shown by groups). However, I want to select the (sub) forum that a new post is submitted to, and this does not work.
I tried many, many, many different things of which none worked, obviously. I'm unable to list all of my attempts here since it's simply too much and partly too long ago. Some of what I tried:
select the drop down by various methods (CSS Selector, class name, XPath, ...)
access the desired option of the drop down directly (by CSS Selector, class name, XPath; even tried to collect arrays of all elements and select the right one by index, didn't work either)
access the desired option of the drop down directly by JavaScript and 'click' it by JS
And now SO is my hope. I thought of a way to select -exactly- this drop down (rather than the other one described) by child elements and so on (this is the reason for the title of this question).
This is the HTML:
<div class="comm-navigation forum-select is-open" id="new-post-forum-select" data-is-open="true">
<a href="#" class="comm-navigation-trigger">
<span class="comm-navigation-name" title="Select forum">Select forum</span>
<i class="comm-navigation-icn-closed foundation-icon-shape-arrow-down"></i>
<i class="comm-navigation-icn-open foundation-icon-shape-arrow-up"></i>
</a>
<ul class="comm-navigation-dropdown hidden" style="width: 519px; display: block;">
<li class="comm-navigation-group forum-list">
<ul>
<li class="comm-navigation-item">
<a href="/communities/forums/100941274" class="comm-navigation-forum" data-forum-id="100941274" data-forum-name="Members" title="Members">
Members
</a>
</li>
<li class="comm-navigation-item">
<a href="/communities/forums/100941276" class="comm-navigation-forum" data-forum-id="100941276" data-forum-name="Q & A" title="Q & A">
Q & A
</a>
</li>
<li class="comm-navigation-item">
<a href="/communities/forums/100941275" class="comm-navigation-forum" data-forum-id="100941275" data-forum-name="Feedback" title="Feedback">
Feedback
</a>
</li>
</ul>
</li>
</ul>
<input type="hidden" name="post[forum_id]" id="post_forum_id" value="">
</div>
I thought of perhaps going from comm-navigation forum-select is-open (which is the same when it's still closed without is-open) to the inner elements to get the right Chrome element and then click the option.
Also, I found that there's a hidden field:
<input type="hidden" name="post[forum_id]" id="post_forum_id" value="">
Which is exactly related to my problem; value is empty as long as nothing's selected, as soon as selected, the forum's id is inserted into the value. This I could imagine if there wasn't any JS that prevents you from submitting the form without having actually selected the sub forums by drop down.
Looking forward to great solutions.
If I understood the question, then first you need to click on the span inside the first link (class=comm-navigation-trigger) with the text "Select Forum". This makes the "ul" below to be visible. Now you need to click the desired "li" inside this list. If so then read on.
Find the span with the css selector - "div[id='new-post-forum-select'] > a[class='comm-navigation-trigger'] > span". Click on this element.
Wait for the list to become visible. Use the css selector - "div[id='new-post-forum-select'] > ul[class*='comm-navigation-dropdown']". Use the ElementIsVisible ExpectedCondition. Though you may or not require this step, added this as a check.
Find the link with the required value - "div[id='new-post-forum-select'] > ul[class*='comm-navigation-dropdown'] a[class='comm-navigation-forum'][data-forum-name='Q & A']". Click on this.

Any DOM parsers that do not modify the DOM?

I need to write a page, can use PHP or .NET, that will display the unmodified html for an element of another page.
The other page may not have valid HTML, but we want it to be returned unmodified. We will not be selecting based on the invalid elements, but will select their parent element and need them returned unmodified.
An example HTML page that my page will be fetching:
<body>
<div>
<p>test1</p>
<br>
<p>test2
<p>test3</p>
</div>
</body>
So far everything I have tried attempts to fix the HTML, it makes the br in the example self closing and the second paragraph tags gets closed.
Is there anything out there that can do this?
Thanks!

How Use From HTMLAgilityPack For Finding A String in nested divs

there is an html codes like below :
<div class="class1 class2 class3">
<div class="class4 class5">
<span class="class6">GOAL STRING</span>
</div>
</div>
now i want to find that GOAL STRING use from HTMLAgilityPack.
how can i do that?
[with LINQ and without LINQ = please show us both ways]
thanks in advance
Well you can use xpath to get the span directly.
document.DocumentNode.SelectSingleNode("//div[#class='class1 class2 class3']/div[#class='class4 class5']/span[#class='class6']").InnerText;
This is a good resource for xpath specifically the table in the middle of the page:
http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C
Also on Google Chrome you can right click -> inspect element and then right click the element that shows up on the tree and click copy as Xpath to get a starting point. These expressions can usually be simplified.

Categories

Resources