I have two buttons, back and next:
By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[1]")
By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[2]")
First goes to previous page, second goes to the next page of my list. When I open my page, first button will be disabled until I get to another page or both will be disabled if my list is short or emty. I need to click those buttons if they are not disabled. Only diference between disabled and not is class attribute:
class="disabled btn btn-plain btn-default-hover"
class="btn btn-plain btn-default-hover"
So how can I check if buttons class atribute contains 'disabled'? scenario goes like this - if second button active click 'next' and then if first button active click 'back'
//el is the web element
if(el.getAttribute("class").split(" ").contains("disabled"))
{
//your code
}
Let's say you have two buttons with the ids "button1" and "button2"..
You can check with something like this:
if(document.getElementById("button1").className.indexOf("disabled") > 1) {
// what to do if disabled...
} else {
// what to do if active...
}
And of course, you can do the same for the other button...
by using jquery write the following code
var ClassAtttr = $("#YourElementId").attr("class");
if (ClassAtttr.indexOf('Your Expression'))
{
//do somthing
}
Why not try the '.Enabled'
WebElement backButton = driver.FindElement('Your Locator');
if(backButton.Enabled)
{
'Your action to be performed
}
Related
I want to click on specific element, but this element is not displayed in current view, clicking on that element fails.
I tried to set focus on needed element before clicking using the following code
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();
But it fails. Can anyone please help?
One of two things. Is this on a popup by chance? If so you need to switch to the iframe.
public static void switchToIframe(string name)
{
_webDriver.SwitchTo().Frame(name);
}
If the element is off the page and it is a scrolling issue you can try this:
You can pass in a value of 100 to move down 100px.
public static void ScrollDownByAmount(string value)
{
var windowScroll = string.Format("window.scrollBy(0,{0})", value);
IJavaScriptExecutor javascript = (IJavaScriptExecutor)_webDriver;
javascript.ExecuteScript(windowScroll , "");
Thread.Sleep(500);
}
I am testing a web page that contains a table. You can click a "Create new" link to add records to the grid. Once "Create New" is clicked, a dialog appears with some text boxes, another grid and a Cancel and Save button. You can then click a link to add a record to the dialog's grid which makes another dialog appear with text boxes and a Cancel and Save button. At my test level class, I currently click on these buttons then wait for the dialog's to open or close. I need to formulate a generic method which encompasses both the click and the wait, for each and every button. So instead of 2 lines of code at my test level to click an element and wait for a window, I would have one line of code that handles that. Below is my dilemma:
I need to be able to apply an If condition where a passed parameter of IWebElement equals a certain IWebElement, but it does not allow me to do this. The if statement doesn't find a match for some reason, so code inside the if statement never gets reached. Am I missing something? If not, is there a workaround?
NOTE: Using button.text == SaveOrganizationBtn.Text is a workaround, but would fail in my specific case, because some of these buttons might not have been loaded into the HTML for a certain test (i.e. A form has not been invoked), so the if statement fails. It would never be able to grab the Text property because it cant find the element in the first place.
Example code:
ClickButton(SaveNetworkBtn);
public void ClickButton(IWebElement button)
{
if (button == SaveOrganizationBtn)
{
SaveOrganizationBtn.Click();
WaitForOrganizationFormToClose();
}
if (button == SaveNetworkBtn)
{
SaveNetworkBtn.Click();
WaitForNetworkFormToClose();
}
Use the Equals() method for your scenario. == will not work for this. you need to check it as if(button.Equals(SaveOrganizationBtn)). The result for this will be true, if it is the same object else it will return false.
I hope, it will help you.
I found a solution, however awkward it might look. I will mark this as the answer if no one else provides a better solution.
ClickButton(SaveNetworkBtn);
public bool ClickButton(IWebElement button)
{
bool buttonClicked = false;
if (Driver.FindElements(By.Id("SaveOrg-Button-ID")).Count > 1)
{
if (button == SaveOrganizationBtn)
{
SaveOrganizationBtn.Click();
WaitForOrganizationFormToClose();
buttonClicked = true;
return buttonClicked;
}
}
if (Driver.FindElements(By.Id("SaveNet-Button-ID")).Count > 1)
{
if (button == SaveNetworkBtn)
{
SaveNetworkBtn.Click();
WaitForNetworkFormToClose();
buttonClicked = true;
return buttonClicked;
}
}
return buttonClicked;
}
I don't fully understand your scenario but it seems like something like this might work... you will have to decide. Instead of passing in button, just check if the button exists on the page, if it does... click it.
ClickButton();
public bool ClickButton()
{
IReadOnlyCollection<IWebElement> saveOrg = Driver.FindElements(By.Id("SaveOrg-Button-ID"));
if (saveOrg.Any())
{
saveOrg.ElementAt(0).Click();
WaitForOrganizationFormToClose();
return true;
}
IReadOnlyCollection<IWebElement> saveNet = Driver.FindElements(By.Id("SaveNet-Button-ID"));
if (saveNet.Any())
{
saveNet.ElementAt(0).Click();
WaitForNetworkFormToClose();
return true;
}
return false;
}
NOTE: .Any() works the same as .Count > 0, I found it a while back and like it a little better but it's a personal preference.
This is my first Page Name: RiskQuery.aspx. From this page on Click the
Link button of Grid View , open another page name, RiskMessage.aspx.
Both pages are under same master Page.
1st Page's Grid View Link button tag below:
<asp:HyperLinkField DataNavigateUrlFields="AppID" DataNavigateUrlFormatString="~\RiskMessage.aspx?AppID={0}" Text="Message" target="_blank" />
code behind of my RiskMessage.aspx:
if (!IsPostBack)
{
string id = null;
try
{
id = Session["cust_id"].ToString();
}
catch { }
if (id != null)
{
txtEntryHomeID.Text = id;
}
getMasterInfo();
}
I successfully opened the 2nd Page and from this page, I update my required
record. But I want to Close the 2nd page after I update the record on the [Save] button press.
I need help closing the 2nd page.
Add this line of code on Save button press:
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>self.close();</script>");
if suppose your page was popup u can close by using window.close()
here u can use instead of
Page.ClientScript.RegisterStartupScript
ClientScript.RegisterClientScriptBlock
otherwise go back one step of your history by using JavaScript
end of your saving code if it is popup
ClientScript.RegisterClientScriptBlock(Page.GetType(), "script", "window.close();", true);
I want to disable clicking and going from one tabPanel to another. Like here http://www.eldan.co.il/en/ the user can't select another step. I tried something like this
if (Tabs.ActiveTabIndex == 0)
{
Tabs.Tabs[1].Enabled = false;
}
but it hides the tab...
I found something that helped me.
...prevent switching to the tab on click depending on form validation
Returning false in the tabs select handler prevents the clicked tab from becoming selected.
$('#example').tabs({
select: function(event, ui) {
var isValid = ... // form validation returning true or false
return isValid;
}
});
Ok, I hope I can explain this well enough.
I have one or more third party Up/Down Spinner+Textbox controls on my page that are black boxes that I can't change the source for.
I want the user to change the UpDownControl contents to choose a quantity and then click a calendar button which will:
Add the quantity of all Up/Down boxes.
Call a javascript popup to display a calendar with the count from step 1 in the url "...calendar.asp?qty=5".
My problem is getting the two steps to execute in the same click. As it stands I can click the button once and it counts
the items and adds them to the popup string and then I have to click it a second time to actually execute the JS popup window.
The code was originally written to "load up" the counts into a second button and then programmatically click it but that looks
like a popup to the browsers since the user didn't click that button.
Here is what I have so far that almost works --
On my page:
<asp:ImageButton ID="btnPrepCal" runat="server" Text="PrepCal" OnClick="btnPrepCal_Click" ImageUrl="~/images/Calendar.gif"/>
In code behind:
public void btnPrepCal_Click(object sender, EventArgs e)
{
StringBuilder sbParams = new StringBuilder();
int TotalQty = 0;
int basketItemCount = 0;
int rowIndex = 0;
string Sku = string.Empty;
foreach (GridViewRow varRow in VariantGrid.Rows)
{
int qnty = GetControlValue(varRow, "Quantity", 0);
if (qnty > 0)
{
basketItemCount++;
string optionList = (string)VariantGrid.DataKeys[rowIndex].Value;
ProductVariant variant = _VariantManager.GetVariantFromOptions(optionList);
if (variant != null)
{
BasketItem basketItem = GetBasketItem(optionList, varRow);
if (basketItem != null)
{
TotalQty += basketItem.Quantity;
Sku = variant.Sku;
}
}
}
rowIndex++;
}
if(Sku.Length > 4) Sku = Sku.Substring(0,4);
sbParams.Append(string.Format("?sku={0}&Qty={1}", Sku, TotalQty));
string popup = string.Empty;
popup = string.Format("window.open('http://trustedtours.org/store/egalaxycalendar.asp{0}','Reservation Calendar','width=265,height=465')",sbParams.ToString());
btnPrepCal.OnClientClick = popup;
}
I'm new to .NET and web programming so I'm probably going at it totally backwards so any help is appreciated. I apologize if it's not clear what I'm trying to do or how. If you need any more info please ask - the rest of the file is a lot of shopping cart mumbo jumbo so I left it out, I hope it's enough.
---- update ----
After looking at the referenced pages I get:
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript>" + popup + "<script>");
cs.RegisterStartupScript(cstype, "PopupCalendar", cstext1.ToString());
And I believe this is added after I set the value of popup near the bottom of my Click handler above, removing the OnClientClick part, right?
Should this popup the other window on a page reload after clicking the button? (I hate being a newb and asking what's probably obvious questions.)
You can accomplish what you're aiming for using the ClientScriptManager.RegisterStartupScript method. Instead of assigning the OnClientClick method of the button to your JS popup code, set that code to run when the page is reloaded using the RegisterStartupScript method.
This page has some good examples: http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx
Ken is correct. To add to his answer and clarify why your code was not working - you were assigning the click-handler of your button to do a popup, but only after it was clicked. This is why you only saw the popup after the 2nd click - the handler was not there the first time you clicked it.