The method getAttribute("value") is not working for textbox - selenium

I am new to selenium. I am trying to retrieve the value of a textbox. Below is my code.
WebElement e = driver.findElement(By.id("id"));
e.sendKeys("text");
String str = e.getAttribute("value");
System.out.println(str);
The above code is working fine in all sites but is not working for a particular site. I can't share the site details.
Any explanation regarding why the code is not working for a site or is there another way to get the text from a textbox?

getAttribute('value') will provide you with null cause html snippet doesnot contains value attribute or the DOM object of that element has no value attribute. Try getting the text with :
String str = e.getText(); //If it helps
OR
use JavascriptExecutor as
JavascriptExecutor js = (JavascriptExecutor) driver;
String str = js.executeScript(
"return document.getElementById('company').value")
.toString();

Related

How can i get the value from div class in TestNG selenium?

I am using TestNG and selenium for testing web app.
<div class="infoMessage">The request has been successfully submitted.</div>
In the my TestNG class file, like any other HTML elments for div element also
I have
#FindBy(xpath="//*[#id='wrapper']/table/tbody/tr[1]/td/div")
WebElement resultdiv;
Now that I got the webelement in resultdiv, how can i read the content "The request has been successfully submitted" ?
Just at a quick glance, it seem like you can try use className instead xpath:
#FindBy(className="infoMessage")
WebElement resultdiv;
Use .getText(); to achieve:
String text = resultdiv.getText();
Hi #bnath002 You can use Contains text Xpath, below is the code. this code always work for text.
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'The request has been successfully submitted.')]"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);
I'm a little unfamiliar with #FindBy, but i'm assuming you could use getText() as normal. But if everything went as planned and the xpath located your element successfully, you should be able to retrieve the text with the following :)
WebElement element = driver.findElement(By.xpath("//*[#id='wrapper']/table/tbody/tr[1]/td/div"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);

How to send values to textboxes that doesn't have input ids in selenium webdriver? I am using phptravles.net for testing?

phptravels location text field
Have attached a image with Html code.
I couldn't able to write a xpath to recognise and sendkeys.
The text field has different xpath when it is focused and when it is not.
Its throwing me NoSuchElementFound exception.
driver.get("http://www.phptravels.net");
WebElement e = driver.findElement(By.xpath("//*[#id='select2-search']/input"));
e.click();
e.sendKeys(city);
You can create xpath using other attributes of that element like
//input[contains(#attribute,'attribute_value')]
Eg. For a input element with class attribute "textbox", xpath can be written like
//input[contains(#class,'textBox')]
I tried with below two points and it worked for me,
1) modified xpath with span text()
2) Instead of e.sendKeys(), tried actions.sendKeys() as the former throwing 'cannot focus' error. Credit to link
driver.get("http://www.phptravels.net");
WebElement e = driver.findElement(By.xpath("//span[text()='Search by Hotel or City Name']"));
/* e.click();
e.sendKeys("GOA");
*/
Actions actions = new Actions(driver);
actions.moveToElement(e);
actions.click();
actions.sendKeys("GOA");
actions.build().perform();

Selenium Java: How can I get the value

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.

How to get the URL of the hyperlink in Selenium driver?

I want to get URL's of hyperlinks present on current page using Selenium Web driver. Can anyone help.
To get the URL of all the links on a page, you can store all the elements with tagname 'a' in a WebElement list and then you can fetch the href attribute to get the link of each WebElement.
you can refer to the following code :
List<WebElement> links = driver.findElements(By.tagName("a")); //This will store all the link WebElements into a list
for(WebElement ele: links) // This way you can take the Url of each link
{
String url = ele.getAttribute("href"); //To get the link you can use getAttribute() method with "href" as an argument
System.out.println(url);
}
Just get them from the href attribute using getAttribute() (assuming you are in java):
WebElement link = driver.findElement(By.tagName("a"))
String url = link.getAttribute("href")

How to grab just element id - using Selenium WebDriver 2

EDIT:
I also tried this
var webElements1 = (Driver.FindElements(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_Control1_lstCategory']//input"))).ToList();
I get the empty Text
I am trying to find a way to grab just ID from the list i am getting and below is my code and a print shot of my screen.
//WebDriver getting a list of Text
the below code returns me the correct number of records but it just give me the Text but I am after Text and Id of an particular Text
I tried this:
var webElements1 = (Driver.FindElements(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_Control1_lstCategory']/tbody/tr/td/span"))).ToList();
this
var webElements2 = (Driver.FindElements(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_Control1_lstCategory']/tbody/tr/td"))).ToList();
and this...
var webElements3 = (Driver.FindElements(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_Control1_lstCategory']"))).ToList();
the all code of line gives me the correct returns but without Id.
Here is the print screen of my page:
After getting all the elements using below method, run in loop to get all element's ids:
List<WebElement> element = driver.findElements(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_Control1_lstCategory']//input")));
for(WebElement ele:elements)
{
ele.getAttribute("id"); // for getting id of each element
ele.getText(); //for getting text of each element
}
1) i'll try to share the idea of the approach I would choose to resolve your issue:
getElementsByTagName('input');//returns array of elements
2) using js executor get element's attribute , ID in particular, iterating through the whole array returned by getElementsByTagName('input') and getting theirs IDs.
//sample of code I used to find color attribute:
public String jsGetColor(String cssSelector){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x=$(\'"+css+"\');");
stringBuilder.append("return x.css('color')");
String res= (String) js.executeScript(stringBuilder.toString());
return res;
}
this is simply my assumtion how it be possible to try. hope it somehow helps you)
If you only need one id:
String id = driver.findElement(By.xpath("//*[contains(text(), 'Your text')]")).getAttribute("id");