Clicking on Href Button selenium - c#

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();

Related

Why does my XPath get stuck on first result when I add /text()?

Here's a simulation of the HTML I am trying to use my XPath on:
<div class="stream-links">
<div>
value I need
</div>
<div>
value I need
</div>
<div>
value I need
</div>
</div>
Now, when I use the XPath pattern //div[#class='stream-links']/div/a in my browser it selects the <a ...> node. Everytime I press enter it selects the next one, but when I use the pattern //div[#class='stream-links']/div/a/text() it gets stuck on the text of the first <a ...> node so when I press enter it does not move to the next. (Using Firebug plugin on FireFox btw to inspect element)
I'm coding a program in C# and the amount of divs under the parent div is a variable so I can't use //div[#class='stream-links']/div[number here]/a/text() because I need to get all of them.
My code for using the Xpath is HtmlNodeCollection NODECOL1 = MEDOC.DocumentNode.SelectNodes("//div[#class='stream-links']/div/a[1]");
So my questions are:
1) Is there a particular reason Firebug doesn't jump to the next <a...> or is it a 'bug' on the plugin's side?
2) Will my code work nevertheless or do I need to approach it in another way?
There're a few things not right with the rest of my code so I can't see if that part of my code actually works or not, wouldn't ask question 2 if I could test it myself right now.
For your HTML, this XPath selects three a elements:
//div[#class='stream-links']/div/a
This XPath selects three text nodes:
//div[#class='stream-links']/div/a/text()
This XPath selects one a element:
//div[#class='stream-links']/div/a[1]
My code for using the Xpath is HtmlNodeCollection NODECOL1 =
MEDOC.DocumentNode.SelectNodes("//div[#class='stream-links']/div/a[1]");
1) Is there a particular reason Firebug doesn't jump to the next
or is it a 'bug' on the plugins side?
//div[#class='stream-links']/div/a[1] only selects one a element.
2) Will my code work nevertheless or do I need to approach it in
another way?
There's a few things not right with the rest of my code so I can't see
if that part of my code actually works or not, wouldn't ask question 2
if I could test it myself right now.
That's not a reasonable question to ask given what you've shown us. Perhaps knowing what the above XPaths return will help you answer it for yourself.

Unable to find ::before css selector element in selenium webdriver

I want to test for checkbox checked or unchecked condition.
This is the html code for checkbox checked
<div classs="input-control checkbox">
<span class="check">
::before
</span>
</div>
::before is css selector.
when i hoverover to checkbox, it shows webelement as span.check::before
but
driver.FindElement(By.CssSelector("span.check::before"));
throws element not found exception.
Any help will be highly appreciated.
In my case, I have removed the pseudo-element ::before from the CSS selector as seen below and it works
Instead of:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.cssSelector("span.check::before"))).build().perform();
I gave:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.cssSelector("span.check"))).build().perform();
Using pseudo-elements in the selector is actually not supposed to work as mentioned here and here.
I'm not sure of your exact html as the comment mentions there is no ::after in your example html. Based on your description this seems similar to the below, but not sure if that is what your exact situation is.
I have utilized the mouse move and then wait for the element that displays the modified css/class. The mouse would move and hover to the first element and then wait for the second to appear on the screen. This works similarly for click and then wait for element to appear.
Hover code example - needs to match your code language and structure...
[Actions Instance goes here].MoveToElement([IWebElementGoesHere]).Perform();
you can also just do
[Actions Instance].Click([IWebElementGoesHere]).Perform();
Reference for this library: https://code.google.com/p/selenium/wiki/AdvancedUserInteractions
I have faced similar issue with pseudo css selectors (::before alike), I have overcome the issue using "Actions" class of selenium java.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.cssSelector("button[id$='save-button']"))).build().perform();
Hope it helps.
Thanks,
Sampath

WatiN finding link to click issue

I am attempting do some automated testing using WatiN and C#. There is a link on the page as an image with the html code
<TD height=27 vAlign=bottom align=right>
<A href="paymentdetailsupdate.do">
<IMG border=0 src="/images/update.gif" width=64 height=21></A></TD>
I just want to click the link but this is proving to be very challenging.
I have tried using following code
cloaspage.Element(Find.By("href", "paymentdetailsupdate.do"));
but it does not seem to work. It times out looking for the element. I have absolutely no idea why. I also can't edit the HTML to add an id or anything like that. Any help is greatly appreciated.
EDIT: I forgot to add in that I can find the link by going through the tables,tablerows, tablecells etc but this is very time consuming and not practical in case of html changes.
Have you tried :
Link link = cloaspage.Link(Find.ByUrl("paymentdetailsupdate.do"));
link.Click();
Probably the same result but you should give it a try...

Unable to decipher DIV tag with CSS

I've got this DIV tag that has a class definition in it.
<div class="clear hideSkiplink">
I've searched the entire project, but I can't find this class anywhere by using the text search feature.
Currently, the DIV is too wide, and I need to trim it down a bit.
Whenever I remove the class="clear hideSkiplink" reference, the DIV tag immediately grows much too large.
I inherited this project after the website developer left. I'm good with C# and WinForms, but not really this web stuff.
Could someone help me out, please?
Solved!
I found .clear { clear: both; } burried in the file StyleSheet.css, but I could not find the hideSkiplink word anywhere in my project.
So, I took the hideSkiplink word off, and the rendered page did not change in the browser.
Apparently, all I was seeing was controlled by the one clear word in the DIV tag.
My tag now reads:
<div class="clear">
Thanks, JLevett!
'clear' and 'hideSkiplink' are too different classes, not one.
Try searching for those individual phrases within your project.
if you have firebug, go to HTML tab, find your element and click on int, I made you a screen of this page as an example, the red circles show the class names and the green circles show you the css source file. You might as well ctrl+click on the source files and they open in a new browser window :)

adding conditional requiredFieldValidators to dynamically created form in c# asp.net

Ok,
i have a fully rendered dynamic form ( i do not know the content of the form, it is provided to my via a webservice )
i used asp.net RequiredFieldValidator for validation, because i read in this article that we could dynamically switch validators on and off depending if the field is visible or not
with the ValidatorEnable(val, enabled) function.
though now that i got the form rendered, i'm running into a bit of trouble with this javascript, as i don't want to put it in the aspx file itself, (don't have a control there anyway since the form is build up in codebehind from the webservice data...)
so i took a look at the clientId and it turns out the validator's client ID is the id of the span it renders to.
so i tried running this in firebug to test if i could enable / disable one of those validators, but that seems not to be possible, a jQuery span element does not have a property to enable it.
ValidatorEnable($("#ContentPlaceHolderDefault_MasterWithNavContent_Poll_4_reqAnswer_373ac8b7_8da9_467b_b9b4_d586e45a7504"), false);
and the html that goes with this
<div class="question-container question-odd" id="ContentPlaceHolderDefault_MasterWithNavContent_Poll_4_question-373ac8b7-8da9-467b-b9b4-d586e45a7504">
<div class="question-meta">
<h3 class="validation-label">Which club have you visited?</h3>
<span style="display: block; margin-bottom: 10px; margin-top: 5px;" class="error validation" id="ContentPlaceHolderDefault_MasterWithNavContent_Poll_4_reqAnswer_373ac8b7_8da9_467b_b9b4_d586e45a7504">Please fill out this field.</span>
</div>
<input type="text" class="answer-container text" id="ContentPlaceHolderDefault_MasterWithNavContent_Poll_4_answer_373ac8b7_8da9_467b_b9b4_d586e45a7504" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$MasterWithNavContent$Poll_4$answer_373ac8b7_8da9_467b_b9b4_d586e45a7504">
</div>
Does someone know where i'm going wrong here?
maybe I'm to quick to jump from the serverside ClientId to the <span> which the RFV renders into? but they seem exactly the same.
hope someone can point me in the good direction!
Maybe a better approach would be to loop through the client-side array of validators (Page_Validators) and find the validator which you want to disable.
See also this MSDN page and this codeproject article for more information.
Perhaps a more appropriate way to do this would be
ValidatorEnable($("<%= reqAnswer.ClientID %>")[0], false);
Using <%= reqAnswer.ClientID %> avoids having to guess at or hard-code the client-side ID of the validator. Adding [0] after the jQuery $() gets the actual validator DOM element instead of the jQuery wrapper.
Source for [0]

Categories

Resources