I am trying to write a script to assert if the text field in question isn't editable. I was attempting to write a verification hidden = "true", because that is what ultimately is hiding the editable text box. How would I be able to verify something like this? and is this the way I should be handling such a verification? Thanks for your responses in advance.
Here is the html
<div id="ctl00_contentMain_sponsorInfo">
<fieldset>
<legend>
Referral Information
</legend>
<ol>
<li>
The name of your referrer is Nameā¦
</li>
<li>
<input id="ctl00_contentMain_sponsorUsername" class="textBig" type="text" hidden="true" style="width:270px;" req="false" max="30" value="Name" name="ctl00$contentMain$sponsorUsername"></input>
</li>
</ol>
</fieldset>
</div>
As you mentioned the best way to test this scenario is to see if the hidden attribute has a value true
IWebElement element = Driver.FindElement(By.Name("ctl00$contentMain$sponsorUsername"));
if (element.GetAttribute("hidden").Contains("true"))
{
Assert.Pass("Your message");
}
Note: You do have to make sure the element exist. You can use some explicit wait with something like
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("something ")));
in order to grab the attribute. This case Selenium will work just fine since you are not interacting with the element.
WebDriver interacts only with the visible elements. You can use JavaScriptExecutor.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string hiddenStatus = (string)js.ExecuteScript("return document.getElementById('ctl00_contentMain_sponsorUsername').hidden");
hiddenStatus will have true/false
Related
I am having a problem being able to check both of these "checkboxes" when trying to run a test for Selenium using C#.
Here is what I have for the first checkbox and that does work and checks the box.
driver.FindElement(By.XPath("//label[#for='LegitimateCompanyAgreementCheckBox']")).Click();
However if I simply use the label for the other checkbox, which is "TermsOfServiceCheckBox", it does not check the box, but it actually opens up a link because the this checkbox has 2 hyperlinks inside the wording.
For example if I try using:
driver.FindElement(By.XPath("//label[#for='TermsOfServiceCheckBox']")).Click();
This will not check the box, it will actually open up the link for /terms-of-service.
Here is the code for both checkboxes I need to check.
<div class="asp-checkbox">
<span class="checkbox gaClick" data-category="companySignup" data-action="legitimateCompanyChecked" data-noninteraction="true">
<input id="LegitimateCompanyAgreementCheckBox" type="checkbox" name="ctl00$MainContent$LegitimateCompanyAgreementCheckBox">
<label for="LegitimateCompanyAgreementCheckBox">I am a legitimate estate sale company or auction company and have the documents necessary to conduct business in my state (if any).</label>
</span>
<label for="LegitimateCompanyAgreementCheckBox"></label>
<span id="LegitimateCompanyAgreementCheckBox_requredCheckBoxValidor" controltovalidate="LegitimateCompanyAgreementCheckBox" style="color:Red;visibility:hidden;">*
</span>
</div>
<span class="checkbox gaClick" data-category="companySignup" data-action="agreeToTermsChecked" data-noninteraction="true">
<input id="TermsOfServiceCheckBox" type="checkbox" name="ctl00$MainContent$TermsOfServiceCheckBox">
<label for="TermsOfServiceCheckBox">I agree to EstateSales.NET's Terms of Service
and Privacy Policy.
</label>
</span>
We need not click on label tag in this case. The actual checkbox is the input tag.
Try Xpath as :: driver.FindElement(By.Id("LegitimateCompanyAgreementCheckBox")).Click();
or
driver.FindElement(By.XPath("//input[#id='LegitimateCompanyAgreementCheckBox']")).Click();
Following is with Wait:-
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 120));
WebElement element = wait.until(ExpectedConditions.ElementIsVisible(By.Id("LegitimateCompanyAgreementCheckBox")));
element.Click();
I have class name active and then there is unique text called active text in span(which is nested). Class name active is the unique among other class names then nested text is unique. How would i click on that. I have used following methods.
FindElement(By.XPath("//li[#class='active']//*[contains(.,'active text')]"));
also i tried
findelement(BY.xpath(//li[#class='active']//div//div//div//span[contains(.,'active text')]"))
also tried this
FindElement(By.XPath("//li[contains(#class,'active')] and //span[contains(.,'active text')]")).Text;
Every time i get no such element found
ANythoughts
this is the html code
<li class="active">
<div class="a">
<div class="b">
<div class="c">
<h1></h1>
<h3 class="d"> some text</h3>
<div class="e">
<span class="f">
Active Text</span>
</div></div></div></div>
</li>
You can use either of the following Locator Strategies:
CssSelector:
FindElement(By.CssSelector("li.active span.f"));
XPath 1:
FindElement(By.XPath("//li[#class='active']//span[normalize-space()='Active Text']"));
XPath 2:
FindElement(By.XPath("//li[#class='active']//span[#class='f' and normalize-space()='Active Text']"));
FindElement(By.XPath("//li[#class='active']//span[contains(text(),'Active Text')]"));
OR
FindElement(By.XPath("//li[#class='active']//span[#class='f' and contains(text(),'Active Text')]"));
Please try the above code. both will work. also, let me know if clarification is required
So what worked for me was this,
FindElement(By.CssSelector("li.active")).FindElement(By.XPath("//span[contains(.,'Active Text')]"));
I just want to know how to get the Xpath Of pseudo Element Actually the thing is that in This I want to .CLick() on this Div Its role Is button so i tried just To xpath with Div Class name it is locating the element but Not clicking i have to Click On this ::after But i cant Get the Xpath Because it is a Pseudo Element
<div id=":l6" class="T-I J-J5-Ji amD T-I-awG T-I-ax7 T-I-Js-Gs L3"
role="button" tabindex="0"
data-tooltip="Older"
aria-label="Older"
style="user-select: none;"
xpath="1">
<span class="amF"
aria-hidden="true">
</span>
<img class="amJ T-I-J3"
src="images/cleardot.gif" alt="">
::after
</div>
What I have read "The Selenium API doesn't support pseudo-elements".
So you can use try using Actions.
I have a sample (I don't have the full path you can add) here with CssSelector:
Actions action = new Actions(Session.Driver);
action.MoveToElement(FindElement(By.CssSelector("div.img::after"))).ContextClick().Build().Perform();
As a last resort, you can use JavaScript with ExecuteScript something like this:
var afterElem = driver.FindElement(By.CssSelector("div.img"));
var buttonAfter = driver.ExecuteScript(
"return window.getComputedStyle(arguments[0],'::after');"
, afterElem);
buttonAfter.click();
You will need to play with the CssSelector Or use an XPath.
Hope this helps you!
I am trying to get a link in a website which changes name on a daily basis. The structure is similar to this (but with many more levels):
<li>
<div class = "contentPlaceHolder1">
<div class="content">
<p>
<strong>'Today's File Here:<strong>
</p>
</div>
</div>
</li>
<li>...<li>
<li>...<li>
<li>...<li>
<li>
<div class = "contentPlaceHolder1">
<div class="content">
<div class="DocLink">
<li>
Download
</li>
</div>
</div>
</div>
</li>
<li>...<li>
etc...
If I find the text (which will remain constant) which is immediately above it in the page by using
IWebElement foundTextElement = chrome.FindElement(By.XPath("//p/strong['Today's File Here:']"));
How can I find the next link in the page by using XPath (or alternative solution)? I am unsure of how to search for the next element after this.
If I use
IWebElement link = chrome.FindElement(By.XPath("//a[#class='txtLnk'"));
then this finds the first link in the page. I only want the first occurance of it after 'foundTextElement'
I have had it working by navigating up the tree to the parent above <li>, and finding the 4th sibling using By.XPath("following-sibling::*[4]/div/div/div/li/a[#class='txtLnk']") but that seems a little precarious to me.
I could parse the HTML until it finds the next occurrence in the html, but was wondering whether there is a more clever way of doing this?
Thanks.
You can try this xpath. It's complicated, as we don't see the rest of the page to optimize it
//li[preceding-sibling::li[.//*[contains(text(),'File Here')]]][.//a[contains(#class,'txtLnk')]][1]
it searches first li which has inside a tag with txtLnk class and it is first found followed after li element with text containing File Here
By.XPath("//a[#class='txtLnk'")
Is a very generic selector, there might be other elements on the page using the same class
You can find this using a CssSelector, try this:
IWebElement aElement = chrome.FindElement(By.CssSelector("div.contentPlaceHolder1 div.content div.DocLink li a"));
Then you can get the href using:
string link = aElement.getAttribute("href") ;
I am writing a website master page with a form for member login
Below is the markup from the master page:
<form id="loginForm" action="account.aspx" method="POST">
<div class="div-topHead">
<input type="button" id="submitLogin" title="Login" value="Login" />
<input type="button" id="forgetPW" title="Forget password" value="?" onclick="window.open('forgetpassword.aspx','_self')" />
</div>
<div class="div-topTail">
<div class="div-login">
<div class="row-body row-def">
<input id="input_memID" name="input_memID" runat="server" type="text" maxlength="255" />
</div>
<div class="row-header row-def">
Member ID:
</div>
</div>
<div class="div-login">
<div class="row-body row-def">
<input id="input_memPW" name="input_memPW" runat="server" type="password" maxlength="255" />
</div>
<div class="row-header row-def">
Password:
</div>
</div>
</div>
</form>
and below is the posting script
$(function(){
$('#submitLogin').click(function(){
var f = $('#loginForm').get(0);
$.post("../script/loginCheck.aspx", { "memID": f.inputID.value, "memPW": f.inputPW.value }, function(data){
var result = JSON.parse(data);
if(result[0] == 1){
//Login validated
f.submit();
} else {
//Login is invalid
alert(result[1]);
}
});
});
});
The problem is, when i open the website on browser,
the text input field names are automatically added a prefix "c100$"
and then from console I can see I got below error
Uncaught TypeError: Cannot read property 'value' of undefined
I have tried to lookup on the net what is this happening,
and seems it is something .NET automatically does.
Yet I could not find a way to make my post script works.
I don't need to keep the ID as it was without ct100,
as long as the posting script can work
I tried changing to f.ct100$input_memID etc but not working
Also tried the clientIDMode="Static" but can't get it work either
the browser complains clientIDMode is not an attribute of <% PAGE %>
So could someone please kindly advise what can I do?
Thanks in advance!
ClientIDMode is a property that can be added to individual controls.
In addition (from MSDN):
You can set the ClientIDMode value for all pages in a Web site by setting the pages element in the site's Web.config file. You can set the ClientIDMode value for a page in the # Page directive.
This for instance:
<div id="dvDiv" runat="server" clientidmode="Static" />
Will make the div's name be "dvDiv" in both client and server side.
So $("#dvDiv") will work fine on the client side.
About your last remark, make sure you put it in <%# Page %> and not in <% Page %> (notice the # sign)
Although Blachshma's answer is perfectly reasonable and will solve your problem, I recommend simply taking off the runat="server" attributes on your inputs.
I can't see that you have any real reason to have these exist as HtmlInputText objects if you don't need to reference them from page code-behind.
EDIT: as per comment, if you do need to access them from code-behind, then it's a better idea to simply change the ClientIDMode to Static.
You can simply use this jquery selector to get any element you want :-
$("[id$='yourElementID']")// Select an Element whose ID ends with yourElementID
Example :-
$("[id$='submitLogin']").click(function(){
var f = $("[id$='loginForm']").get(0);
....................
See The Detailed Information here
If the Javascript code is contained inside the aspx markup, you can also use
var f = $('#<%= loginForm.ClientID %>').get(0);