I'm using Selenium to test a webpage, and I can't seem to figure out how to write the XPath for the Next button.
There are multiple Next buttons in the code, however they're in different sections. The one I'm trying to test has the following code:
<section class="col-md-12">
<input type = "submit" value ="Next">
If I use the following command it gives me an unidentified element error.
//input[#type = 'submit']
OR
//input[#value = 'Next']
This is probably because it's viewing the other next buttons in the page. I'm not sure how to proceed.
This will select the input elements (with #value attribute value equal to Next) anywhere beneath section elements (with #class attribute value equal to col-md-12):
//section[#class='col-md-12']//input[#value='Next']
Related
I have a form where each text box has a title. I already have the WebElement of the text boxes, and I want to reference their title (the title that has "for=id" pointing to them).
I've tried their getText which only returns the text in the input box, tried getCssValue("label") which doesn't return with anything. I've tried finding all the labels but that doesn't help as I'd still have to sift through all of them, and find the 8 different labels that need to have the *.
<label for="customer_firstname">First name <sup>*</sup></label>
<input onkeyup="$('#firstname').val(this.value);" type="text" class="is_required validate form-control" data-validate="isName" id="customer_firstname" name="customer_firstname" value="">
I want to get back a string of the label text so I can check (with .contains())if it contains the "*"at the end. Preferably with as little Xpath as possible.
to indicate label tag using input tag, you can use this xpath :
//input[#id='customer_firstname']/preceding-sibling::label
if you just want input field then you should use id which I think is unique in your case, though you will have to verify this in DOM.
id = customer_firstname
For referencing with <label **for**>
you can use this code :
String custLabelAttribute = driver.findElement(By.xpath("//input[#id='customer_firstname']/preceding-sibling::label")).getAttribute("for")
this should print : customer_firstname
if both Input and label tags are belongs from same parent then you can take first unique xpath of parents and then adding // you can reach up to the label tag and get text. you should reach up to label tag by find unique xpath and use .gettext() then u will get innertext of label tag.
Basically there are 3 types of Save button in a page. Now, the button which I'm trying to click on is a type="button" and remaining types of save are not defined as type="button".For all three save buttons LinkText is defined as Save. So, is there any way to click on type="button" by linkText.
HTML:
<button class="btn btn-md bgm-blue m-r-10 waves-effect" ng-click="updateUser()" type="button">Save</button>
Code which I tried:
List<WebElement> list = Util.getWebDriver().findElements(By.xpath("//*[text()='Save']"));
System.out.println("SaveButton"+list.size()); ///Returning 3 save button in a page
list.get(3).click();
Now, suppose in one page there are 4 save buttons and in another page there are 3 save buttons. So, it is not possible to make a method because each time index will differ.
If there is any way to find xpath by type="button". Will be easy for me to make a method and call it each time I want to click on"Save".
Please let me know in case of clarification.
is there any way to click on type="button" by linkText
Actually By.linkText() is use to locates <a> elements only that contain the given link text while you're trying to locate <button> element. So you can not locate desire element using By.linkText().
button which I'm trying to click on is a type="button" and remaining types of save are not defined as type="button"
As you are saying only desire button contains attribute type="button", So it is very easy to find that element using other locator as below :-
By.cssSelector() :
button[type='button']
button[type='button'][ng-click='updateUser()']
button.btn.btn-md.bgm-blue.waves-effect[type='button'][ng-click='updateUser()']
By.xpath() :
//button[text()='Save' and #type='button']
//button[.='Save' and #type='button']
//button[text()='Save' and #type='button' and #ng-click='updateUser()']
//button[.='Save' and #type='button' and #ng-click='updateUser()']
As the Save button contains class attribute you can construct an xpath as follows:
findElements(By.xpath("//button[#class='btn btn-md bgm-blue m-r-10 waves-effect'][contains(text(),'Save')]"));
How to handle this kind of pop-up. My goal is to get the message when Submit button clicked then validate it againts my own text (maybe using assert). I've tried to locate the element using firepath (xpath) but when i click locate Element button on firebug, the pop-up disappear.
Here is the screenshot of the pop-up.
popUp
Here is the code :
<p class="errors"></p>
<input id="email" class="form-control" type="email" value="" name="email" required="" oninput="setCustomValidity('')" oninvalid="this.setCustomValidity('Email Cannot Be Empty')" placeholder="Email *" data-placeholder="X" data-format="">
Thank you in advance.
From the scrrenshot it looks like a tooltip. Something like when we mouse over Google title in https://www.google.co.in/.
To verify tooltip we can get the attribute 'title' and verify.
Example : in https://www.google.co.in/. tooltip is placed in title attribute as below.
title="Google"
<div id="hplogo" style="background-size:272px 92px;height:92px;width:272px" title="Google" onload="window.lol&&lol()" align="left">
For your scenario, the displayed tip message is available in 'oninvalid' attribute as below. So get this attribute value and validate it.
oninvalid="this.setCustomValidity('Email Cannot Be Empty')"
If the element is not inside a iframe, then you can directly try as follows:
String emailId = driver.findElement(By.id("email")).getText()
// write string equals login here comparing emailId that is captured and the one you want to compare to.
if not, first find the iframeand switch to it and then use above code to find the element. More detailed answer related to switching b/w frame is here
Finding the elements in the Pop-up:
Instead of clicking on the Locate Element button (of Firebug) first, Right click on the element you want to find in the Pop-up, and select Inspect with Firebug, which gives the corresponding HTML code for the element.
This is a bit late in the game but the way you get the custom validity message and not the generic one you have to call the reportValidity() event within JavaScript in Selenium. You'll see a driver.executeScript() method and this is where you must call the reportValidity event on the element being validated. This is how I did it:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions
.ElementToBeClickable(By.CssSelector("input#NewPassword.form-control")));
IWebElement input = driver.FindElement(By.CssSelector("input#NewPassword.form-control"));
input.SendKeys(string.Empty);
IWebElement form = driver.FindElement(By.TagName("form"));
form.Submit();
driver.ExecuteScript("document.getElementById('NewPassword').reportValidity();");
Assert.AreEqual("New password required", input.GetAttribute("validationMessage"));
I'm trying to locate and click an element on my page but can't use the by.id method as the id's are generated and change per session. For most elements I can get around this by using xpath but there is a dropdown menu where this does not work. I can click the element containing the dropdown and it shows me the options. If I locate the element I need and copy it's xpath the test case won't function stating it can't find the xpath. Now next to the id the Element I'm trying to click also has a class. Problem is that this class is not unique, all menu items in the dropdown have the same class with a different text. What I would like to do is something like:
driver.findeElement(By.class("x-menu-item-text").equals("Unique text 1here").click()
The class "x-menu-item-text" is not unique but the text in this particular class is. I can't use the ID as this is automatically generated. The full code for the item or element I want to click is:
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 1 here</span></a>
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 2 here</span></a>
I'm using Selenium Webdriver with Eclipse (Java).
Allthough the answer provided seems to work on most pages and locations, there is a situation however where I can't get it to work. Can anyone advise?
There is a page with buttons and I want to click one of these buttons. If I use the following statement:
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Add']")).click();
I get an error message
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
If I look at the source I see:
<button class=" x-btn-text" id="ext-gen539" type="button">Add</button>
So the element is present and visible.
I've tried adding a wait.until statement before the click statement but this does not work either:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")));
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")).click();
Extra information: could this problem be because I'm looking for an element that is located in a popup?
You can use xpath locator
https://newcircle.com/bookshelf/selenium_tutorial/locators
By.xpath("//span[#class='x-menu-item-text' and text()='Unique text 1here']")
How can I access this element:
<input type="submit" value="Save as XML" onclick="some code goes here">
More info: I have to access programmatically a web page and simulate clicking on a button on it, which then will generate a xml file which I hope to be able to save on the local machine.
I am trying to do so by using HtmlUnit libraries, but all examples I could find use getElementById() or getElementByName() methods. Unfortunately, this exact element doesn't have a name or Id, so I failed miserably. I supposed then that the thing I have to do is use the getByXPath() method but I got completely lost into XPath documentation(this matter is all new to me).
I have been stuck on this for a couple of hours so I really need all the help I can get.
Thanks in advance.
There are several options for an XPATH to select that input element.
Below is one option, which looks throughout the document for an input element that has an attribute named type with the value "submit" and an attribute named value with the value "Save as XML".
//input[#type='submit' and #value='Save as XML']
If you could provide a little bit more structure, a more specific (and efficient) XPATH could be created. For instance, something like this might work:
/html/body//form//input[#type='submit' and #value='Save as XML']
You should be able to use the XPATH with code like this:
client = new WebClient(BrowserVersion.FIREFOX_3)
client.javaScriptEnabled = false
page = client.getPage(url)
submitButton = page.getByXPath("/html/body//form//input[#type='submit' and #value='Save as XML']")
Although I would, in most cases, recommend using XPath, if you don't know anything about it you can try the getInputByValue(String value) method. This is an example based on your question:
// Fetch the form somehow
HtmlForm form = this.page.getForms().get(0);
// Get the input by its value
System.out.println(form.getInputByValue("Save as XML").asXml());