How to get values from disabled inputs in webdriver - selenium

My html code is -
<input id="txtPortalLogin" class="form-control input-sm" type="text" disabled="disabled" placeholder="No Link" value=""/>
Please assist some code to get the values from disabled field.
Screenshot is attached so that you will find the which text values i am talking about.
Input Fields are disabled and I want values from shown screenshot

I know how to get this value with Python code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('your_page_url')
disabled_input_field = driver.find_element_by_id("txtPortalLogin")
value = disabled_input_field.get_attribute('value')

Use the javascript executor in selenium to execute a javascript code which return the value of the html element ( input). Something simmilar to the below
String value = "";
if (driver instanceof JavascriptExecutor) {
String value = (String)((JavascriptExecutor) driver)
.executeScript("return document.getElementById('txtPortalLogin').value");
}

In C# it would be
driver.FindElement(By.Id("txtPortalLogin")).GetAttribute("value");
however you could also do this using the JavaScript executor as so:
var value = ((IJavascriptExecutor)driver).ExecuteScript("return $('#txtPortalLogin').attr('value')").ToString();
EDIT; I think the OP might want to get the https://production etc part out of the field. I'm not 100% sure how to do that, sorry.
I did also find this link which might be able to answer your problem; TLDR is make it readonly instead of disabled which allows the value behind to still be accessible but not changeable by the user.

Usually driver.getAtribute("value"); works. But as value field is empty, you'll have to go for JavaScriptExecutor -
JavascriptExecutor je = (JavascriptExecutor) webDriver;
String value = je.executeScript("return angular.element(arguments[0]).scope().{{modalValue:put modal value from HTML}};", {{webElement}}).toString();
return value;

Related

How to find prompt by Selenium

Does anybody know how to find this type of element by Selenium? (to validate its presence or text)?
I tried to catch it as alert (swithToAlert()) but it doesn't work. Any ideas? It is also can not be inspected as element and I can't find it in Elements. Thank you in advance.
This uses HTML5 form validation. This is created by the browser, and does not exist in the DOM. Therefore Selenium cannot see this.
You can access this using JavaScript. Here is a brief code sample:
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement field = driver.findElement(By.tagName("input")); // your input box
if (!(Boolean) jsExecutor.executeScript("return arguments[0].validity.valid;", field)) {
return (String) jsExecutor.executeScript("return arguments[0].validationMessage;", field);
}
The entire API is documented.
element.validity.valid
Returns true if the element's value has no validity problems; false otherwise.
So this popup is displayed if this returns false, but only after clicking Submit on the form.

No value attribute in DatePicker input box

I have to scrape this site and for that first I need to input data in the fields. I am using Selenium library for the task.
http://nhb.gov.in/OnlineClient/categorywiseallvarietyreport.aspx?enc=3ZOO8K5CzcdC/Yq6HcdIxJ4o5jmAcGG5QGUXX3BlAP4=
The page source has the following code in which I want to input the date in the Date input box.
<div style="float: right;">
<input name="ctl00$ContentPlaceHolder1$txtdate" type="text"
id="ctl00_ContentPlaceHolder1_txtdate" style="width:100px;" />
</div>
But there happens to be no value attribute and when I try to send input using the driver.send_keys() method nothing happens.
This is what I've tried but it doesn't work.
date = driver.find_element_by_id("ctl00_ContentPlaceHolder1_txtdate")
date.send_keys('09/12/2018')
I have even tried mouse operations using ActionChains module but the Date input box is not clickable.
Is there anything that I'm doing wrong here?
I tried it your way, and it worked for me. Not sure why it's not working with you.
Here's the code I used with what you provided:
from selenium import webdriver
url = 'http://nhb.gov.in/OnlineClient/categorywiseallvarietyreport.aspx?enc=3ZOO8K5CzcdC/Yq6HcdIxJ4o5jmAcGG5QGUXX3BlAP4='
driver = webdriver.Chrome()
driver.get(url)
date = driver.find_element_by_id("ctl00_ContentPlaceHolder1_txtdate")
date.send_keys('09/12/2018')
And it worked.
Maybe, try using .find_element_by_name and use "ctl00$ContentPlaceHolder1$txtdate" ??
url = 'http://nhb.gov.in/OnlineClient/categorywiseallvarietyreport.aspx?enc=3ZOO8K5CzcdC/Yq6HcdIxJ4o5jmAcGG5QGUXX3BlAP4='
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_name("ctl00$ContentPlaceHolder1$txtdate").send_keys('09/12/2018')

How can i write my own xpath from the html code

I have followig HTML code and want X path for the text "Analytics & Research"
<div id="LLCompositePageContainer" class="column-wrapper">
<div id="compositePageTitleDiv">
<h1 class="page-header">Analytics & Research</h1>
</div>
I am getting following xpath using chrome, but that didnt work.
//*[#id="compositePageTitleDiv"]
this is my code
WebElement header = driver.findElement(By.xpath("//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/h1[#class='page-header']"));
String header2 = header.getText();
System.out.println(header2);
and following error I am getting
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath ==
//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/h1[#class='page-header']
(WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.34 seconds For documentation on this
error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Please try to use the below xpath:
driver.findElement(By.xpath(".//div[#id='compositePageTitleDiv']/h1")).getText();
If the element is inside the iframe. Then use the below code:
// Switching to the frame
driver.switchTo().frame(<name>);
// Storing the value of the Analytics & Research
String text = driver.findElement(By.xpath(".//div[#id='compositePageTitleDiv']/h1")).getText();
// Switching back to original window
driver.switchTo().defaultContent();
Hope this helps.
This is how it can be used :
WebElement element= driver.findElement(By.xpath("//*[#id='compositePageTitleDiv']"));
Or in case it is nested, can be accessed like this as well
WebElement element = driver.findElement(By.xpath("//html/body/div[3]/div[3]/"));
this is just a rough syntax.
No need to use Xpath here if you could simply locate the element using By.id(). Asuming are using Java, you should try as below :-
WebElement el = drive.findElement(By.id("compositePageTitleDiv"));
String text = el.getText();
Edited :- If element not found, may it is timing issues you need to implement WebDriverWait to wait for element until visible on the page as below :-
WebDriverWait wait = new WebDriverWait(webDriver, implicitWait);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("compositePageTitleDiv")));
String text = el.getText();
Note :- if your element is inside any frame, you need to switch that frame before finding element as :- driver.switchTo().frame("your frame name or id");
Hope it helps..:)
You can also use
//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/
h1[contains(text(),'Analytics')]
This is the best way to reach to the specific web element, using contains minimize the chances of error.
The correct xpath is
//div[#id='LLCompositePageContainer']
/div[#id='compositePageTitleDiv']
/h1[#class='page-header']
But you could have find your answer easily with some researchs on google...

How to get the span class text using selenium webdriver

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".

Getting the default value (text) of an <input> element with selenium webdriver

I wonder about how I can use selenium webdriver to find the default text of an element ?
In the browser, the input field displays a default value: 'Project 1', but I cannot get this text through the method getText() of this WebElement.
<input class="title viewData" id="sprojectName" maxlength="255" name="projectName" type="text" projectinfo="1">
getText() returns "the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace." You need something like getAttribute("value") or getAttribute("placeholder").
The getText() method is for retrieving a text node between element tags for example:
Eg:
<p>New</p>
But usually the value in the text box is saved to "value" attribute. So the below statement will work:
findElement(By.id("ElementID")).getAttribute("value");
Yes, I will try to see if getAttribute("value") work. In the meantime, I have solved the problem using JavaScript executor:
String jsStatement = "return document.getElementById('" + elementId + "')." + "value" + ";";
JavascriptExecutor js = null;
if (session instanceof JavascriptExecutor) {
js = (JavascriptExecutor)session;
}
return (String) js.executeScript(jsStatement);