Selenium (python) text deletes on its own - python

Selenium is able to find these elements and send_text to them, but right after the text is written it is automatically deleted. Am I calling .send_text() wrong, or am I finding the wrong element to interact with?
This is a popup frame dialog, but I can't seem to focus on the frame. I'm not even sure I need to as selenium still interacts with the text fields.
Here is the javascript:
<div class="control-group">
<label class="control-label">Patient ID</label>
<div class="controls">
<input data-ng-model="subject.Code" data-custom-error="Patient ID already exists in this center" class="ng-valid ng-dirty" data-original-title="" type="text">
<i class="required-field" style="top:inherit">*</i>
</div>
</div>
Here is one method I have been using:
patientID = 'testID'
driver.find_element_by_xpath("//input[#data-ng-model='subject.Code']").send_keys(patientID)
also,
driver.find_element_by_css_selector(".controls > input[data-ng-model='subject.Code']").send_keys(patientID)
As I said, both find the correct element and send text but the text is immediately deleted. Any ideas, I'm stuck.
I'm using firefox, but the same happens in chrome.

Have you checked if there is any javascript code in the page listening to changes to the input field and deleting its contents?

Related

How to access multiple text boxes in selenium

i am trying fill the text boxes. Each time you get into the page the the number of text boxes will be different and the name of them will be different.
<div class="fb-single-line-text">
<div class="display-flex" data-test-single-line-text-input-wrap="true">
<!---->
<input name="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)" id="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)" class="ember-text-field ember-view fb-single-line-text__input" aria-required="true" aria-describedby="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)-error-message" data-test-single-line-text-input="" type="text">
</div>
</div>
the above lines are the html code of first text box
<div class="fb-single-line-text">
<div class="display-flex" data-test-single-line-text-input-wrap="true">
<!---->
<input name="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257389,numeric)" id="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257389,numeric)" class="ember-text-field ember-view fb-single-line-text__input" aria-required="true" aria-describedby="urn:li:fs_easyApplyFormElement:(urn:li:fs_norm
alized_jobPosting:2620509178,30257389,numeric)-error-message" data-test-single-line-text-input="" type="text">
</div>
</div>
So i see all of them class name in comman so i tried to get all the eemphasized textlemnts inside the class . but i could not find a possible way for it. can someone help me with that
There are multiple ways to deal with this situation.
use find_elements to find out how many input boxes are present in UI, also you could send different message to those input box.
total_no_of_input_with_div_single_line_text = len(driver.find_elements(By.CSS_SELECTOR, "div.fb-single-line-text input"))
print(total_no_of_input_with_div_single_line_text)
Now you can send_keys to individual elements like this :-
total_no_of_input_with_div_single_line_text[0].send_keys('some string')
you can always change the index above from 0 to 1, 2 etc.
second in a loop like this :
for ele in driver.find_elements(By.CSS_SELECTOR, "div.fb-single-line-text input"):
ele.send_keys('some value here')
other way would be to use xpath indexing to locate the element.
(//div[#class='fb-single-line-text']/descendant::input)[1]
should locate the first web element input in the page, also you have the access to change that to [2], [3] and so on..

How do I select this dropdown button in Selenium?

I have this html :
<div class="dropdown float-right">
<button class="btn btn-default" type="button" id="export-button" data-toggle="dropdown">
<i class="the-download-export"></i>
Export
</button>
<div class="dropdown-menu dropdown-menu-right"><div class="dropdown-item action-export" data-format="csv" data-store="com" data-reverse-type="single"><i class="text-primary fa fa-upload" aria-hidden="true"></i>Download File</div>
This is a dropdown that has a few options. When the button is clicked, it collapses the dropdown selection and the divs appear(there are more divs for various data formats which i haven't added here for simplicity).
I have tried a few things in order to click the button with the csv data format, but everything has failed. Either not finding it or getting the div only, which is not interactible.
What is the proper way of selecting and clicking this dropdown collapsible button in Python ?
That's very little to work on.
I can recommend either checking whether you can get the data another way (e.g. api call when clicking the button) or something with x-path like
button = getFromXPath(".//button[#id='export-button']")
button.click()

Python and Selenium Search for Text Outside of Tags

I'm trying to make a selenium script using python that is able to search for a specific text and click on it. Below is an example of the code I'm working with. In this example I'm searching for the string "Next" and clicking on it.
<div class="btn-container text-center">
<button id="btnNext" class="btn btn-lg btn-primary btn-block">Next</button>
</div>
So far I have tried the following:
driver.find_element_by_xpath("//*[#id='btnNext']").click()
driver.find_element_by_id('btnNext').click()
driver.find_element_by_xpath("//*[contains(text(),'" + 'Next' + "')]").click()
driver.find_element_by_xpath("//button[contains(.,'Next')]").click()
But no luck so far. Any suggestions? Thank you for all the help!

Python+Selenium Scrape Error: ElementNotVisibleException

I'm currently trying to use Selenium/Python/PhantomJS to scrape the results of the form below:
http://gis.vgsi.com/newhavenct/Sales.aspx
It looks like when I try to click on the search button, I get an ElementNotVisibleException error.
My code:
self.driver.find_element_by_id("MainContent_btnSearch").click()
After some digging online, it seems like the button may be hidden. Indeed, here is the relevant HTML code from the search page:
<input type="button" value="Search!" class="btn btn-primary searchTrigger" style="width: 200px;" />
<input type="submit" name="ctl00$MainContent$btnSearch" value="Search" id="MainContent_btnSearch" style="width: 200px; display: none;" />
<div id="MainContent_ctl00" style="display:none;">
</div>
I tried preceding my previous code with a click of the searchTrigger, but this is still not working:
self.driver.find_element_by_class_name("searchTrigger").click()
self.driver.find_element_by_id("MainContent_btnSearch").click()
Any advice would be greatly appreciated!
Requested element has attribute style="display:none;", so you need to make it visible
Try to use following code:
self.driver.execute_script('document.getElementById("MainContent_btnSearch").style.display="block";')
self.driver.find_element_by_id("MainContent_btnSearch").click()
A portion of the screen is not visible by Selenium. It may be that another element lies on top of the one you are trying to click.
For me what always works without spending hours searching for the cause is to click using Javascript:
self.driver.execute_script("arguments[0].click();", elt)
where elt is a WebElement - e.g. returned by find_element_by_id(.).
Make it a function and use wherever the problem occurs.
Edit: it is a generic answer for when it happens again, but in this particular case the other answer is probably correct.

Unable to upload image/file in python using selenium webdriver

I'm unable to upload and save an image.
The following is the piece of code that facilitates file upload using the UI. At first, only the upload button is visible and once an image is selected from the uploader the save button is visible.
<td colspan="1" class="actions">
<input type="button" class="button" name="logo" id="logo-f-upload" value="Upload Image"/>
<input type="file" id="logo-upload" accept="image/*" name="logo-upload" style="display:none" />
<input type="submit" value="click to save" name="submit_logo_now" class="main submit_dm logo-save-focused" style="display: none"/>
</br>
</td>
I tried driver.find_element_by_id("logo-f-upload").send_keys(os.getcwd()+"/image_test.png")
and also
driver.find_element_by_id("logo-upload").send_keys(os.getcwd()+"/image_test.png")
but it doesn't throw any error at this stage but on the next where it says "element is not visible ..." ie. the save button. On the UI, when simulating this, the file uploader doesn't open but the file_upload button value changes to the path of the image and then breaks.
I believe this is the answer which is merely a JS hack: So the problem is that the logo-upload input has style set to display:none and due to this, selenium can't find the element. The solution that works for me is using javascript to set the style to display:block, after which using send_keys(img_path) works fine.
dr.execute_script("document.getElementById('logo-upload').setAttribute('Style','display:block')")

Categories