I have a span as below:
<div class="ag-cell-label">
<span class="glyphicon glyphicon-asterisk" title="This is a draft row. It can only be seen by you. "/>
</div>
I want to get the text "glyphicon glyphicon-asterisk". How can I do it.
The validation of the test case is to check weather asterisk is not present after clicking on save button.
Assuming you are using Java, You should try as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='ag-cell-label']/span")));
String class = el.getAttribute("class");
Hope it will help you...:)
This is a simple case of:
Locate the WebElement using a suitable locator strategy (Class, CSS, XPath etc) and assign it to a new WebElement object.
Use the .getAttribute(String arg) method with an argument of "class" to retrieve the required class value from the WebElement object instantiated in the first step and assign it to a new String object.
Use the .contains(String arg) method with an argument of "asterisk" to determine whether the "class" attribute retrieved in the second step contains the text "asterisk".
Related
In Selenium Webdriver we have three Text boxes. All the text boxes have the same Id, and I want to send some text in the second text box.
This is my code:
Driver.findElements(By.xpath("//*[#id='testInstanceScan']"));
Could anyone please help how to handle Text boxes which have same Id ?
Currently i m using below code which always send same text for all the three text boxes.
List<WebElement> textfield1 = Driver.findElements(By.xpath("//*[#id='testInstanceScan']"));
for(int i=0; i<textfield1.size();i++){
WebElement local_textfield1=textfield1.get(i);
String value1=local_textfield1.getAttribute("placeholder");
if(value1.equalsIgnoreCase(""))
{
local_textfield1.sendKeys("Amarendra Singh");
}
}
There are two ways to get the second textbox.
Method 1: using find elements
List<WebElement> textfields = Driver.findElements(By.xpath("//*[#id='testInstanceScan']"));
textfields.get(1).sendKeys("Amarendra Singh");
Method 2: using xpath
WebElement textfield2 = Driver.findElements(By.xpath("(//*[#id='testInstanceScan'])[2]"));
textfield2.sendKeys("Amarendra Singh");
You can use an array like trick to access each element matching the XPath
WebElement textField1 = driver.findElements(By.xpath("(//*[#id='testInstanceScan'])[1]"));
WebElement textField2 = driver.findElements(By.xpath("(//*[#id='testInstanceScan'])[2]"));
The quickest and easiest way I know to do this is to use an XPath to specify the expected ID and placeholder = "" and then specify the index of the element you want.
Given an HTML example of
<html>
<div id="testInstanceScan" placeholder="">e1</div>
<div id="testInstanceScan" placeholder="">e2</div>
<div id="testInstanceScan" placeholder="">e3</div>
</html>
The code below works
List<WebElement> searchBox = driver.findElements(By.xpath("//div[#id='testInstanceScan'][#placeholder='']"));
System.out.println(searchBox.get(1).getText());
and returns
e2
which is the element that contains the desired ID, empty placeholder attribute value, and is the second element. This form of XPath can be tailored to a lot of different situations to get the desired element in a single pass instead of looping or filtering the collection.
In your case, your final code would look like
List<WebElement> textfields = driver.findElements(By.xpath("//input[#id='testInstanceScan'][#placeholder='']"));
textfields.get(1).sendKeys("Amarendra Singh");
If that still doesn't work, you may need a wait.
By locator = By.xpath("//input[#id='testInstanceScan'][#placeholder='']");
List<WebElement> textfields = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
textfields.get(1).sendKeys("Amarendra Singh");
I don't know exactly what is yoyr question but what i understand you want to perform operation on textbox which have same xpaths
This is a sample Html which I used for example
<html>
<div id="test" placeholder="">element1</div>
<div id="test" placeholder="">element2</div>
<div id="test" placeholder="">element3</div>
<div id="test" placeholder="">element4</div>
</html>
First use findElements() to extract all elements and then use if loop to select one and perform operation This is my code
WebDriver driver=new FirefoxDriver();
driver.get("URL");
List<WebElement> allElements=driver.findElements(By.xpath("//div[#id='test']"));
for (WebElement tempElement : allElements) {
if(tempElement.getText().equalsIgnoreCase("element2"))
{
System.out.println(tempElement.getText()); // you can perform your operations
}
}
Using firefox and marking a link in my web-app I get among other things, this code which I think I can use to caprture an object:
cb_or_somename_someothername cb_area_0219
This string is "classname" in Firebug.
Going to the script I type in:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername cb_area_0219"));
But the script does not find the element when executing.
Other onfo in the Firebug panel is:
class="cb_or_somename_someothername cb_area_0219"
onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole \':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false"
href="#"
id="fwMainContentForm:j_idt156:2:selectRole"
Is my script referring the element in a wrong way?
You cannot use search by compound class name (name with spaces). Try to use search by CSS selector instead:
WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename_someothername.cb_area_0219"));
or by XPath:
WebElement rolle = driver.findElement(By.xpath("//*[#class='cb_or_somename_someothername cb_area_0219']"));
Also you still can use search by one of two class names:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername"));
or
WebElement rolle = driver.findElement(By.className("cb_area_0219")); // Note that this class name could be generated dynamically, so each time it could has different decimal part
Update
If you get Element is not clickable... exception it seem that your element is covered by another element at the moment you try to click on it. So try to wait until this "cover" is no more visible:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[#class='environtment-banner']"));
WebElement rolle = driver.findElement(By.className("cb_area_0219"));
rolle.click();
I have a Catalogue Number text field, which has a value = Pen populated.
When I inspect the element in DOM tree, the textfield value is data bind.
How can I get the data bind value and verify the textfield is not empty using Selenium Java?
<input id="txtCatalogueNo" class="k-textbox" maxlength="25" data-bind="value: selectedCatalogue.CatalogueNumber">
Thanks
Try,
WebElement TxtBoxContent = driver.findElement(By.id("txtCatalogueNo"));
System.out.println(TxtBoxContent.getAttribute("value"));
or
WebElement TxtBoxContent = driver.findElement(By.id("txtCatalogueNo"));
System.out.println(TxtBoxContent.getText());
You can get the value of any attribute of a WebElement using getAttribute method.
If data-bind attribute contains the value,
WebElement TxtBox = driver.findElement(By.id(txtCatalogueNo));
System.out.println(TxtBox.getAttribute("data-bind")); // to get the value in data-bind attribute.
If Value attribute contains the value:
System.out.println(TxtBox.getAttribute("value")); // to get the value in data-bind attribute.
you can retrieve the test using getText method
System.out.println(TxtBox.getText()); // to get the text
Try this:
WebElement TxtBox = driver.findElement(By.id("txtCatalogueNo"));
String valueTxtBox=TxtBox.getAttribute("data-bind").split(":")[1].trim();
System.out.println(valueTxtBox);
OR
WebElement TxtBox = driver.findElement(By.id("txtCatalogueNo"));
String valueTxtBox=TxtBox.getAttribute("data-bind").replace("value: ", "");
System.out.println(valueTxtBox);
valueTxtBox will contain the value you are looking for.
Sorry for the late reply on this its pretty simple. I'll just assume you are using angular.
WebElement element= driver.findElement(By.id("txtCatalogueNo"));
String content = (String) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].value", element);
Hopefully this works for you.
The HTML code is given attached, I do not want to use hard code xpath, the requirement is to make it generic:
<td bgcolor="#FFFFFF">
<input name="hotel_name_0" id="hotel_name_0" type="text" value="Hotel Creek" class="select_text" onfocus="disable_ctrlV()" onkeypress="return Nothingonly(event)">
</td>
Code:
public static boolean fncVerifyTextInColumn(WebElement gridObjWebElement,
String stringToValidate, int columnNumber,String colName) {
boolean flagTextinColumn=false;
ArrayList<WebElement> objRows;
ArrayList<WebElement> objCols;
ArrayList<WebElement> childElement;
objRows=(ArrayList<WebElement>)gridObjWebElement.findElements(By.tagName("tr"));
objCols=(ArrayList<WebElement>)objRows.get(0).findElements(By.tagName("td"));
if(objCols.get(columnNumber).getText().equalsIgnoreCase(colName)){
for(int index=1;index<objRows.size();index++){
objCols=(ArrayList<WebElement>)objRows.get(index).findElements(By.tagName("td"));
childElement=(ArrayList<WebElement>)objCols.get(columnNumber).findElements(By.xpath("//input"));
System.out.println(childElement.get(0).getAttribute("value"));
if(stringToValidate.trim().equalsIgnoreCase(childElement.get(0).getAttribute("value").trim())){
flagTextinColumn=true;
}
}
}
return flagTextinColumn;
}
Method Calling:
fncVerifyTextInColumn(objGrid,hotels,1,"Hotel Name");
I would use cssSelector [id^='hotel_name_'] to locate the element and then getAttribute() retrieve the attribute value
By css = By.cssSelector("[id^='hotel_name_']");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(css));
System.out.println(myDynamicElement.getAttribute("value"));
Notice the regex search of cssSelector here. With ^ I am skipping any dynamic number. Hoping that's the only element with hotel_name_someNumber on the page.
Just do
String attValue = driver.findElement(byAnyMethod).getAttribute("AttributeName");
Hope it helps
I think what you are looking for is this. (I'm assuming you know how to code, you just need a general direction so I'm going to leave out specific code.)
First, find the table the td is in. You might need to use an xPath for this or you'll need to assign an ID to the table so you can locate it.
Then once you have the table, do a FindElements to get the list of TRs under it.
Once you have the TRs, you can loop through them, grab the TDs under that and grab the TD at the index that has the INPUT you want the value of, get the INPUT and then get it's value.
Yep, lots of steps.
A shortcut may be to class all of the inputs you want the value for with a unique class and do a FindElements By className and loop through that list.
Can I take all local-name() of WebElement?
I don't know the attributes of this webElement and I wish to find and save it.
Edit:
I have WebElement elm which it tagName is Div. I find this element by the next command:
WebElememnt elem = driver.findElements(By.xpath(//*[identifier="c")).get(1)
Now, I want to know all the attributes of this element, which I don't know when I do my query. for example: elm is the next in my DOM:
<div identifier="c" someAtr="b" someAtr2="c">
So I wish to know that I have an attributes which it name is "someAtr"="b" and "someAtr2"="c" (Again, I don't know that someAtr even exists, and for that I want all the attributes).
You can do so using the Javascript Executor and then getting the innerHtml:
String html = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", ele);
Then you can parse the String as you normally would.