I've got a script writing values into a web page, and all values write except for one field that keeps throwing up the following error:
(Screenshot provided b/c in other similar questions many comments said this is impossible to happen on a web page.)
"Please enter a numeric value."
Here's my code:
workcenter_to_add = {}
workcenter_to_add['BatchCycle'] = str(2.78)
# driver = my_chrome_webpage
WebDriverWait(driver, wait_time).until(EC.presence_of_element_located((By.XPATH, "//input[#id='BatchSize']"))).send_keys(workcenter_to_add['BatchCycle'])
As everyone knows, if I do not input the 2.78 value in as a string WebDriver throws an error. But my page demands a numeric value. I'm stuck.
I've Googled around and not found a usable answer to this. It seems if you're using Java there's a setAttribute method you can use, but if you're using Pythonyou've got to figure something out.
For example, the question here looked promising but I could not find the String or how to import it to get it to work. There's a couple of other much older questions that talk about executing java but I have had no luck getting them to work.
I've got the page-source HTML here:
https://drive.google.com/open?id=1xRNPfc5E65dbif_44BQ_z_4fMYVJNPcs
I am sure though you are passing the value .send_keys('2.78'), still the value will be numeric. So, ideally you should not get this issue.
Here is the sample html and script to confirm the same.
<html><head>
<script>
function validateOnClick(evt) {
var theEvent = evt || window.event;
// Handle paste
if (theEvent.type === 'click') {
key = document.querySelector('input').value.toString();
} else {
// Handle key press
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
}
var regex = /[0-9]|\./;
console.log(key);
if( !regex.test(key) ) {
alert("Please enter numeric value");
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
</head>
<body>
<input placeholder='check'></input>
<button type='submit' onClick='validateOnClick(event)'>Submit</button>
</body></html>
Script to check:
driver.get(url)
# check with string (not integer)
driver.find_element_by_tag_name('input').send_keys('Hello')
driver.find_element_by_tag_name('button').click()
print(driver.switch_to.alert.text)
driver.switch_to.alert.dismiss()
# now check with integer
driver.find_element_by_tag_name('input').clear()
driver.find_element_by_tag_name('input').send_keys(workcenter_to_add['BatchCycle'])
driver.find_element_by_tag_name('button').click()
Screenshot:
So, We have to check what's the js/method implemented to validate the value entered in the field. As you can see passing integer with in quotes from python script does not make any difference to the field and it's data type.
I'm sure this is going to be an unpopular answer, but this is how I got it work.
The field in this question and another field on another page in the same ERP system were throwing the same error. send_keys() would not work no matter what I tried.
That's when I put on my thinking cap and starting trying other ways.
I tried entering the information into another field on the page that would accept numbers via send_keys() and then cutting and pasting the values into the field that would not accept the value had I used send_keys(). It worked!
Here's a code snippet I used on the different page with the same issue:
elem1 = driver.find_element_by_id('txtNote')
elem1.send_keys(rm['txtGross_Weight'])
elem1.send_keys(Keys.CONTROL, 'a') #highlight all in box
elem1.send_keys(Keys.CONTROL, 'x') #cut
elem2 = driver.find_element_by_id('txtGross_Weight')
elem2.send_keys(Keys.CONTROL, 'v') #paste
I was looking for a high tech answer when a low tech work around sufficed.
Is it code or methodology I'd write on a job resume? Probably not. Did it work and can I live with it? Yes. Thank you for the people who tried to answer.
It looks a lot like a comma vs dot problem?
But I could be wrong.
It depends on the browser locale of the machine that your selenium is running on.
So a simple test could be to enter the text '2,78' or '2.78' into the field.
Python converts the number to a string, and that is not a localized number.
When it is sent as keys, it is sent as four characters '2' '.' '7' '8'.
It then ends in the Javascript scope of your browser, that depending on the OS and Language settings will be either using comma or dot as a decimal separator.
The dialog box with the notification
possibly is the outcome of Constraint API's element.setCustomValidity() method.
I had been through the page-source HTML which you have shared. But as per your code trials:
By.XPATH, "//input[#id='BatchSize']"
I didn't find any <input> tag within the pagesource. The text based relevant HTML would have helped us to construct an answer in a better way. However, you need to consider a few things as follows:
As you are dealing with an <input> tag, instead of presence_of_element_located() you should be using element_to_be_clickable().
You haven't told us about the error WebDriver throws if you do not input the 2.78 value as a string. Still, as str(2.78) works so you can stick to it.
Effectively your line of code will be:
workcenter_to_add = {}
workcenter_to_add['BatchCycle'] = str(2.78)
WebDriverWait(driver, wait_time).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='BatchSize']"))).send_keys(workcenter_to_add['BatchCycle'])
References
You can find a couple of relevant discussions on Constraint_validation in:
How to handle html5 constraint validation pop-up using Selenium?
How can I extract the text of HTML5 Constraint validation in https://www.phptravels.net/ website using Selenium and Java?
Related
I'm using Python and Selenium to fill out a web form. On one of the pages, it has multiple input fields with the same ID. Sometimes it's 1, 2, 3 and 4. Each ticket is different.
Here's my attempt:
how_many_meter_options = len(browser.find_elements_by_xpath("//span[contains(#class,'meterdisplay')]"))
browser.implicitly_wait(30)
print("There are ")
print(how_many_meter_options)
print(" Meter Options")
thecountofmeters = str(how_many_meter_options)
for row_of_meters in thecountofmeters:
browser.find_element_by_xpath("//span[contains(#class,'meterdisplay')]").click()
thelastmeter = browser.find_element_by_id("meteredit-lastreading-display").text
print(thelastmeter)
browser.implicitly_wait(30)
browser.find_element_by_id('meteredit-display').clear()
browser.find_element_by_id('meteredit-display').send_keys(thelastmeter)
browser.implicitly_wait(30)
browser.find_element_by_name('ok').click()
browser.implicitly_wait(30)
This only fills out the first input field. I need it to do all.
Here's the html
<input rt-autofocus="" type="number" id="meteredit-display" name="display" data-ng-model="meter.MeterDisplay" min="0" data-ng-disabled="!canEditMeter()" class="ng-pristine ng-valid ng-valid-number ng-valid-min ng-valid-pattern">
Here's my attempt:
The function find_element_by_id() will only find the first element with a matching id that you put in the arguments. To my knowledge there is no function to find multiple using just the id as an argument, however you might be able to use find_elements_by_xpath("//input[id='meteredit-display']") which will return a group of elements you can iterate through and apply your commands on.
Something like this:
input_elements = browser.find_elements_by_xpath("//input[id='meteredit-display']")
for element in input_elements:
element.clear()
element.send_keys(thelastmeter)
Let me know if you try this and how it works.
Edit: Also I should add that calling browser.implictly_wait() multiple times does nothing. The functions tells the webdriver to wait up to the specified amount of time when trying to find something before it moves on. It is something that just needs to be set once and then you don't have to call it again unless you want to change the amount of time that is waited.
Like Nathan Roberts suggested above, you can look for the xpath, however, having multiple elements with the same id is not considered valid HTML. If you have any say in this I'd recommend requesting the change.
Another option would be to use a regular expression on the raw html.
I just want to say that I am new to Python/Selenium and I've been googling and wracking my head for hours now.
I am trying to extract a value from an element from the website we use for creating tickets at work. The thing is, the element with the ticket number has no ID and I cannot seem to be able to single it out using its other attributes.
The HTML for the elements is as follows:
<span class = "custom-field__value ng-binding ng-scope"
ng -
if = "!$parent.editMode && data.value" tooltip = ""
ng - bind - html = "((status.isCollapsed) ? (data.value | securelinky : '_blank')) || ('customField.label.noValue' | i18n)"> INC22963815
</span>
So, I want to get the INC22963815 from the element.
I tried with
ticket = driver.find_element_by_class("custom-field__value ng-binding ng-scope")
ticket = driver.find_elements_by_class("custom-field__value ng-binding ng-scope")
for number in ticket:
print(number.get_attribute("innerText"))
but I get nothing.
But when I tried with
ticket = driver.find_elements_by_tag_name("SPAN")
for number in ticket:
print(number.get_attribute("innerText"))
I found the text I am looking for, but it also outputs almost everything else on the page, so it is not very useful.
So now I am resorting to asking the question myself. I need help just to single out that one element. Thank you in advance.
Have you considered using the element's XPATH? You can use the Chropath plugin https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo, to get the element's XPath.
You can then do something like
driver.find_element_by_xpath("xpath_string_here")
Let me know if this helps!
I see you are using class name (that has spaces in it). Class name does not support spaces. so that's why it did not work. Moreover it's just a combination of multiple class separated by a space.
consider changing it to css selector :-
ticket = driver.find_element_by_css_selector("span.custom-field__value.ng-binding.ng-scope")
P.S :- it's not recommended to use automatic xpath or css generated by browser built in tools.
I want to fill out some personal data on a website. The first input element can be accessed by find_element_by_id but the id of the next text field has a different id every time I access the website. In a Browser, I can simply press the TAB key two times to get to the desired text input field. Is there a way to achieve similar behavior with selenium?
i tried the following:
input1 = browser.find_element_by_id('id_email')
input1.send_keys("email#something.com")
input2 = browser.send_keys(Keys.TAB).send_keys(Keys.TAB)
input2.send_keys("Something else")
But Line 3 gives me:
AttributeError: 'WebDriver' object has no attribute 'send_keys'
You cannot send keys against the browser object.
This line:
input2 = browser.send_keys(Keys.TAB).send_keys(Keys.TAB)
is not valid. It even tells you as such: 'WebDriver' object has no attribute 'send_keys'
That says your webdriver object (which you called "browser") does not have a attribute ( a method/ a function) called Send_Keys.
Top tip to avoid this sort of problem is to use a good IDE with intellisense. That will tell you the methods you can use.
In vscode, you get this:
As you type, it tells you valid commands - and you can see the driver has no send keys!
What you need to do is use .sendKeys(..) against a web element.
In your code you already have input1 - that is a web element. You can send keys against that.
Something like this:
input1 = browser.find_element_by_id('id_email')
input1.send_keys("email#something.com")
input1.send_keys(Keys.Tab)
If you want to do multiple tabs from the same object, you can just add multiple. This will tab 3 times
.send_keys(Keys.TAB + Keys.TAB + Keys.TAB)
When i run a sample script on google.com,
I tab 3 times i go to the googlesearch button. (first tab is the clear, second tab is the microphone, 3rd tab goes to the button):
Finally, using tabs to navigate is a LAST RESORT. They can be flaky and inconsistent. You REALLY should get an identifier for your object.
If you can share your URL or the page DOM then I'm help you identify a working identifier. I know you say there is no ID but there many ways to access objects.
welcome!
i can only guess, since i'm not able to test the code out. But seems like you are not getting the right element with the 2 Tabs, since it starts from the start of the html page and not from the input1 field. You'd better getting the password by its id or parents id, rather then with Key.TAB.
eleUserMessage = driver.find_element_by_id("xxxxxxx")
eleUserMessage.send_keys(email)
Im trying to use selenium with python to auto fill out a form and fill in my details. So far I have read in my info from a .txt file and stored them in variables for easy reference. When I Find the element and try to fill it out with send_keys, after each send_keys line, the form highlights the other fields that aren't filled in and says I need to fill them in before I submit. My code to submit the info is way after this code segment.
Why does send_keys try to submit the form or even send the enter key when I didn't tell it to? and how do I stop this from happening?
The main issue that this is causing is that the element ids change when they are highlighted in red since they are required fields. I need to get around that somehow. Please let me know what I can do.
Because you are storing your details in a text file, it is likely that when you create the email variable there is a newline at the end of the string as this is how text files work. This would explain why the form gets submitted because it is the equivalent of typing the email followed by the enter key. You can try to fix this by using
eleUserMessage.send_keys(email.rstrip())
rstrip() is a builtin function and it by default, with no parameters, strips the whitespace and newlines from the right side.
if you just want to fill out a form ,then submit the finished form.
you can try :
eleUserMessage = driver.find_element_by_xpath("//select[#name='name']")
all_options = eleUserMessage.find_elements_by_tag_name("option")
for option in all_options:
print("Value is: %s" % option.get_attribute("value"))
option.click()
eleUserMessage.send_keys(email)
I would like to get exact text inside tag with selenium and python.
When I inspect the element, I can see the html below on the browser.
<div class="value ng-binding" ng-bind="currentEarning">£8.8</div> == $0
I have written the python code with selenium in order to get text as follows.
currentEaring = Ladbrokes.find_element_by_xpath('//div[#ng-bind="currentEarning"]').text
When I run this script several times, I occasionally get the result as 0 - this is not true.
Rarely I can get £8.8 - this is ture.
I guess I occasionally get 0 because of the == $0 but not sure.
How can I get the text as £8.8? - using regex? If then, how?
it's happening may be because, it takes some time to populate the text after page has loaded, and it seems like you are not waiting enough.
you can use explicit wait to wait until element contains certain text.
For your case, following example might work.
wait = WebDriverWait(driver, 30)
wait.until(EC.text_to_be_present_in_element((By.XPATH, "//div[#ng-bind='currentEarning']"), "£"))
Here is the Answer to your Question:
One of the reason to get improper results may be due to asynchronous rendering of the HTML DOM due to presence of JavaScript and AJAX calls. As you have taken help of the ng-bind attribute only so our intended node may not be the unique/first match in the HTML DOM. Hence we will refine our xpath to be more granular & unique by adding the class attribute along with ng-bind attribute and take help of get_attribute method to get the text £8.8 as follows:
currentEaring = Ladbrokes.find_element_by_xpath('//div[#class="value ng-binding" and #ng-bind="currentEarning"]').get_attribute("innerHTML")
Let me know if this Answers your Question.