No value attribute in DatePicker input box - selenium

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')

Related

Invalid xpath expression to find a slider element

Very new to Katalon Studio and selenium. Trying to write an automated test using selenium webdriver to change the value of the slider on the webpage.
I am failing to locate the element. Somehting is wrong with my findelement statement. Also once I locate the slider element successfully I don't know how to change the value of the slider. Can you help? or provide some quidance?
<div class="bdr-slider"
<span id="bdrText">bdr Amount</span>
<input min="0" max="15" step"0.5" value="5" type="range">
</div>
WebElement Slider = driver.findElement(By.xpath("*[div(#class,'bdr- slider')]"))
Try this :
WebElement Slider = driver.findElement(By.xpath("//div[#class='bdr-slider']"))
I'm not familiar with specifics of mobile automation, but I think that you need to locate the input element.
This one
<input min="0" max="15" step"0.5" value="5" type="range">
and then maybe call sendKeys method on that element.
You can try this line of code:
driver.findElement(By.xpath("//div[#class='bdr-slider']/input")).sendKeys("10");
Before proceeding, make sure the element is really present in the DOM, and thus making sure the problem is not with the path, enter $x('//div[#class="bdr-slider"]/input') to console in DevTools and see if you can locate the element.
If you can find the element, since you are using Katalon Studio, you can do this:
TestObject slider = new TestObject().addProperty('css', ConditionType.EQUALS, '.bdr-slider input')
or, if you prefer xpath:
TestObject slider = new TestObject().addProperty('xpath', ConditionType.EQUALS, "//div[#class='bdr-slider']/input")
You will need to import these two:
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject

Why do I get a timeout although webelement is visible selenium?

In selenium I have the following code
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#placeholder='Create new collab']")))
in order to select an input field inside the following construct:
<div class="md-input-container md-theme-default md-input-placeholder">
<label>Collab Name</label>
<input placeholder="Create new collab" class="md-input" type="text">
<!---->
<!---->
<!---->
::after
</div>
But I get a timeout exception after a wait for 10 seconds. Manually I am able to click and type something in that input field a second after is is being loaded.
The following ExpectedConditions are unable to find the element:
visibility_of_element_located
element_to_be_clickable
while this methid is able to find the element:
presence_of_element_located
but I am not able to use send_keys to the input field. I get an ElementNotInteractableException error. Also 'clicking' in the element before does not work - same error.
So what to try else?
The div with class - 'md-tabs-wrapper' has a style attribute - 'transform: translate3d(-748px, 0px, 0px);'. This div contains the input box that you are trying to interact. What the transform:translate3d does it is shift the divs and contents to the left and outside the viewport of the browser. You can test it by copying the relevant div in browser and then switching off this style or changing the values.
This kind off explains why the "presence" EC works but visibility and click EC do not. Apparently selenium is not able to figure that element is visible and thus throws the ElementNotInteractableException.
Use ActionChains instead. Use the element from the presence EC.
actions = ActionChains(driver)
actions.move_to_element(element)
actions.send_keys("hello world")
actions.perform()
Also need to clear the existing placeholder text for the sendkeys to work properly.
Try the following:
elem = driver.find_elements_by_xpath("//input[#class='md-input' and #type='text']")
driver.execute_script("arguments[0].scrollIntoView();", elem)
wait = WebDriverWait(driver, 10)
my_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#class='md-input' and #type='text']")))
my_element.click()

Xpath not working as expected while running the test

I am trying to automate the browser, while I try to locate the element via xpath in browser in static mode it is able to highlight the element where as when I run the script it comes back with an error that it is unable to find the element.
xpath I have written:
driver.findElement(By.xpath("//input[#value='soa_b_pbtv_l0_trnkni']/following-sibling::td[1]/child::select[#name='jobaction']")));
Here is the HTML:
<form name="f2" onsubmit="return verify();" action="/ATS/cgi-bin/barcap_jobaction.pl" method="post">
<>
<input name="jobname" type="hidden" value="soa_b_pbtv_l0_trnkni"/>
<input name="jobinstance" type="hidden" value="D03"/>
<input name="jobproceed" type="hidden" value="web"/>
<td style="background-color: #ffffff;">
<select name="jobaction">
if you're trying to select the select, jobaction then try this:
use css selector for the select select[name='jobaction']
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='f2']")));
List<WebElement> eleList = driver.findElements(By.cssSelector("form[name='f2']")));
for(WebElement element: eleList) {
if(element.findElement(By.cssSelector("input[name='jobname']")).getText().equalsIgnoringCase("expectedValue")) {
WebElement element = element.findElement(By.cssSelector("select[name='jobaction']"));
}
}
The INPUT is hidden so it won't be found using typical Selenium means. Selenium was designed to only interact with elements that a user can see and interact with. You are able to locate it in the browser because you are using JS or JQuery and they are not designed to ignore hidden elements. One way to get around this is to use JavascriptExecutor... it basically allows you to run JS in Selenium and find hidden elements. Since it sounds like you already have successful locators, I would suggest you look up some tutorials on JSE and you should be set.
If you run into a new issue while using JSE, come back and post a new question and we can try to help you.

Selenium, Autoit and iframe

I was trying to automate the control on a page, on where there is a iframe and an element that can be controlled with AutoIT. I need to click the Scan button within the iframe. I used driver.switch_to.frame("frmDemo") to switch frame, but it seemed not working. Any idea please?
Here is the code:
import win32com.client
import time
from selenium import webdriver
autoit = win32com.client.Dispatch("AutoItX3.Control")
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("http://example.com")
time.sleep(2)
driver.switch_to.frame("frmDemo")
scanButton = driver.find_element_by_css_selector('body.input[type="button"]')
scanButton.click()
input is not class, its child element of body. Try without body
scanButton = driver.find_element_by_css_selector('input[type="button"]')
You can also try by the value attribute
scanButton = driver.find_element_by_css_selector('value="Scan"')

How to get values from disabled inputs in webdriver

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;