Choose month from drop down using selenium controlled Mozilla on python - python

I want to make an automated Gmail account creator. For some reason, I am not being able to web driver to click on the month and choose a month to proceed with the creation.I'm using an earlier version of selenium(2.4) and Firefox(25) because of having problems with gecko driver.
<label id="month-label" class="month">
<span id="BirthMonth" class=" form-error" aria-invalid="true">
<div class="goog-inline-block goog-flat-menu-button jfk-select" role="listbox" style="-moz-user-select: none;" aria-expanded="false" tabindex="0" aria-haspopup="true" title="Birthday" aria-activedescendant=":0">
<div id=":0" class="goog-inline-block goog-flat-menu-button-caption" role="option" aria-setsize="12" aria-posinset="0"> … </div>
<div class="goog-inline-block goog-flat-menu-button-dropdown" aria-hidden="true"> … </div>
</div>
<input id="HiddenBirthMonth" type="hidden" name="BirthMonth"></input>
</span>
</label>
I have filled all of the earlier fields which didn't require a dropdown.
birthelem2=(browser.find_element_by_id('BirthMonth'))
birthelem2.click()
month=browser.find_elements_by_class_name('goog-menuitem-content')
month[0].click()
I expect it to choose a month(January)

Related

Selenium python how to upload file when there is no input type file?

I am using Selenium python to automate a site. The problem I have face is, I have to upload file but there is no input type file available where I could have been using send_keys() method.
The File upload element:
<div id="data-assets-interior-file-upload" data-upload-properties="{"formId":"form-main-1","path":"data[assets][interior]","modalUpload":"Uploading...","warnExtensionsStrings":{"pdf":"<div class=\"a-box a-alert-inline a-alert-inline-warning\"><div class=\"a-box-inner a-alert-container\"><i class=\"a-icon a-icon-alert\"><\/i><div class=\"a-alert-content\">\n Most PDF files do not produce great results in an automated conversion process. We recommend using a Word, Mobi, ePUB or HTML file if you have one. <a href=\"\/en_US\/help\/topic\/A2GF0UFHIYG9VQ?ref_=_fg\" target=\"_blank\" rel=\"noopener noreferrer\">Learn more.<\/a>\n <\/div><\/div><\/div>\n <div id=\"file-warn-actions\" class=\"a-form-actions a-spacing-none a-spacing-top-large\">\n <div class=\"a-row a-spacing-none\">\n <div class=\"a-column a-span6\">\n <span class=\"a-declarative\" data-action=\"potter-file-warn-extension-continue\" data-potter-file-warn-extension-continue=\"{}\">\n <span id=\"file-warn-extension-continue\" class=\"a-button a-button-base button-fill\"><span class=\"a-button-inner\"><button id=\"file-warn-extension-continue-announce\" class=\"a-button-text\" type=\"button\">\n Continue with PDF\n <\/button><\/span><\/span>\n <\/span>\n <\/div>\n <div class=\"a-column a-span6 a-span-last\">\n <span class=\"a-declarative\" data-action=\"potter-file-warn-extension-cancel\" data-potter-file-warn-extension-cancel=\"{}\">\n <span id=\"file-warn-extension-cancel\" class=\"a-button a-button-primary button-fill\"><span class=\"a-button-inner\"><button id=\"file-warn-extension-cancel-announce\" class=\"a-button-text\" type=\"button\">\n I have another format\n <\/button><\/span><\/span>\n <\/span>\n <\/div>\n <\/div>\n <\/div>","pdf-header":"Do you have another format?"},"acceptedExtensions":"doc,docx,zip,htm,html,mobi,azw,epub,rtf,txt,pdf,kpf","multipart":null,"persistSuccess":true,"warnExtensions":["pdf"],"key":"save","url":"\/en_US\/title-setup\/kindle\/A3U1YUNVSBYTMZ\/content\/action\/save","workflowId":"assets.interior","assetType":"DIGITAL_BOOK_BLOCK"}" class="a-section jele-file-field">
<div class="a-section a-spacing-none file-upload-options-section">
<p class="a-spacing-small">
</p>
<div class="a-row file-upload-extra-info-message-section">
<div class="a-column a-span12">
<div class="a-box a-alert a-alert-info"><div class="a-box-inner a-alert-container"><i class="a-icon a-icon-alert"></i><div class="a-alert-content">Use Kindle Create to transform your manuscript to an eBook with professional book themes, images, and Table of Contents. Click here to download for free.</div></div></div>
</div>
</div>
<br/>
<div class="a-row file-upload-browse-section">
<div class="a-column a-span12">
<span class="a-declarative" data-action="browse-clicked" data-browse-clicked="{"signInRequired":false,"id":"data-assets-interior-file-upload"}">
<span id="data-assets-interior-file-upload-browse-button" class="a-button a-button-primary file-upload-browse-button"><span class="a-button-inner"><button id="data-assets-interior-file-upload-browse-button-announce" class="a-button-text" type="button">
Upload Book
</button></span></span>
</span>
<span class="a-declarative" data-action="file-selected" data-file-selected="{}" id="data-assets-interior-uploader">
<span class="fileuploader a-hidden"></span>
</span>
<p class="a-spacing-top-small a-size-mini a-color-tertiary a-text-italic">
</p>
</div>
</div>
<input type="hidden" name="" value="doc,docx,zip,htm,html,mobi,azw,epub,rtf,txt,pdf,kpf" id="data-assets-interior-file-upload-accepted-extensions" class="accepted-extensions"/>
</div> </div>
Can anyone let me know, how to handle this scenario? If you are gonna recommend me some other library for it then please post relevant examples in python as well. Thank you

Find Elements with corresponding information in Selenium python

I am designing a bot which will shortlist and send mail to specific candidates on the basis of resume database. A website with a collection of resumes and user can search related resumes on the basis of keywords and location.
With chrome Driver and selenium I got upto a point where entering keyword and location website shows some results. Here is the point where I am having problem
driver.find_elements_by(class_name, css_selector, name, tag_name, xpath) I tried all of them to retrieve the list of results and corresponding items like, name of candidate, email, id_of_that_resume and also need to handle if I already have sent the candidate invitation mail or not.
It will be a great favor if you answer with the code. Thanks in advance
this is the image corresponding to the inspected element alongwith the search result of resume against keyword
<div class="col-xs-12 tuple ng-scope" data-ng-class="resumeCtrl.GetCssClass(item.Status)" ng-repeat="item in resumeCtrl.items | limitTo: resumeCtrl.limitToValue track by $index" ng-show="resumeCtrl.items.length > 0" style="">
<div class="checkbox" style="margin-top: 57px;">
<input id="chk131545341" name="resumerowcheckbox" type="checkbox" onchange="global.isChecked(this);" ng-click="resumeCtrl.isAllChecked()" ng-checked="" data-itemid="131545341" data-isviewed="false" data-itemtitle="Dimple AXXXXXXX" data-itemcity="" data-statustext="" data-itemstatuscd="" data-documentid="131545341" data-firstname="Dimple">
<label for="chk131545341"></label>
</div>
<div class="tuple-body mob-qs-wrap">
<div class="col-xs-2 quick-section text-right pull-right visible-xs"><a class="quicklinks pull-right"><i class="fa fa-ellipsis-v fa-2x"></i></a></div>
<div class="tuple-col col-xs-10 col-sm-6 info">
<h3 data-isviewed="false" class="tuple-title">
<span class="link ng-binding" ng-click="resumeCtrl.ShowResume(item,resumeCtrl.FromRange+$index);">Dimple A</span>
</h3>
<address class="h5 text-dark-gray">
<span data-ng-show="resumeCtrl.showLocationInfo(item)" class="ng-binding ng-hide">, </span>
<span data-ng-show="resumeCtrl.showMilesInfo(item)" class="ng-binding ng-hide"> - miles</span>
</address>
<!-- ngIf: resumeCtrl.showExperienceInfo(item.Experience) --><ul class="list-info text-dark-gray h5 ng-scope" data-ng-if="resumeCtrl.showExperienceInfo(item.Experience)">
<li class="ng-binding">Last Active: 01/07/2020</li>
<li class="ng-binding">Work Experience: 20 years 1 month</li>
</ul><!-- end ngIf: resumeCtrl.showExperienceInfo(item.Experience) -->
<!-- ngIf: !resumeCtrl.showExperienceInfo(item.Experience) -->
<p class="mt15 text-dark-gray fs12 hidden-xs p0 ng-binding" data-ng-bind-html="item.HighlightingSnippet | to_trusted">Enterprise Applications and Database Development using , <strong>Python</strong>... . Hands on experience in data processing automation using <strong>python</strong> .... patterns . Web development...</p>
</div>
<div class="tuple-col col-xs-10 col-sm-4 hidden-xs">
<a data-ng-click="resumeCtrl.ShowResume(item,resumeCtrl.FromRange+$index)" data-preview="preview" class="btn btn-primary preview">Reviewed</a>
<span class="h5 ng-hide" data-ng-show="item.IsViewed">Last viewed </span>
</div>
<div class="tuple-col col-xs-10 col-sm-2 hidden-xs quick-section mob-quick-section">
<div class="action-toggle">
<button class="btn btn-default bg-default" data-toggle="dropdown"><span class="text">Actions<i class="caret"></i></span></button>
<ul class="action-menu dropdown-menu">
<!-- Start Forcefull link add-on for mobile view, should REFRAIN taking this approach -->
<li class="hide">
<a data-ng-click="resumeCtrl.ShowResume(item);" data-preview="preview" class="link quicklinks ng-binding" data-maction="PRVW">
<span class="fa-icon-width"><i class="fa fa-file-text-o"></i></span>Preview Resume
<span class="h5 text-gray ml25 mb10 lht1 ng-hide" data-ng-show="item.IsViewed">Last viewed </span>
</a>
</li>
<!-- End Forcefull link add-on for mobile view, should REFRAIN taking this approach -->
<li><a class="link quicklinks" data-maction="INVT" data-ng-click="resumeCtrl.InviteToApply(item, 'Rimeiko Lyles');"><span class="fa-icon-width"><i class="fa fa-envelope"></i></span><span class="quick-text">Invite to Apply</span></a></li>
<li><a class="link quicklinks" data-maction="ADCN" data-ng-click="resumeCtrl.AddCandidateToJob(item, 'Rimeiko Lyles');"><span class="fa-icon-width"><i class="fa fa-user-plus"></i></span><span class="quick-text">Add Candidate to Job</span></a></li>
<li><a class="link quicklinks" data-maction="DNLP" data-ng-click="resumeCtrl.DownloadResume(item, 'pdf')"><span class="fa-icon-width"><i class="fa fa-file-pdf-o"></i></span><span class="quick-text">Download to PDF</span></a></li>
<li><a class="link quicklinks" data-maction="DNLD" data-ng-click="resumeCtrl.DownloadResume(item, 'docx')"><span class="fa-icon-width"><i class="fa fa-file-word-o"></i></span><span class="quick-text">Download to MS Word</span></a></li>
</ul>
</div>
</div>
</div>
</div>
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
#assuming you defined driver already.
some_elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located(
(By.XPATH, "//div[#data-ng-class='resumeCtrl.GetCssClass(item.Status)']")))
for element in some_elements:
print("element.text:", element.text)
Or
same_elements = driver.find_elements_by_xpath("//div[#data-ng-class='resumeCtrl.GetCssClass(item.Status)']")
for element in same_elements:
print("same_element.text:", element.text)

Can selenium find_element_by_xpath in outlook online?

I'm having trouble finding elements by xpath or id inside of outlook's online html. I'm using selenium and python. Here is what I've wrote.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException #imports
browser = webdriver.Chrome('C:\\Users\\...')
browser.get('https://www.office.com/...') #setup
#then some code to sign into outlook online
browser.find_element_by_xpath('//*[#id="_ariaId_64"]/div/div/div[1]')
browser.find_element_by_xpath('//*[#id="MailFolderPane.FavoritesFolders"]/div[19]')
browser.find_element_by_id('_ariaId_68') #attempts to find an element in outlook
this is the element I'm trying to access. I would post the entire html, but there is just so much. I'm brand new to all things code so go easy on me :)
<div>
<div id="_ariaId_68" aria-expanded="false" draggable="true" dropzone="string:text/plain">
<div autoid="_n_R" class="_n_44 canShowFavoritesAction" role="treeitem" aria-expanded="false" aria-labelledby="_ariaId_68.folder _ariaId_68.ucount" aria-haspopup="true" tabindex="-1">
<div class="_n_S3 nowrap border-color-transparent _n_X3" style="padding-left: 4px;">
<div class="_n_V3 _n_54">
<span autoid="_n_S" class="_n_W3 ms-font-m ms-fwt-sl _n_Y3" id="_ariaId_68.folder" title="Sent Items">Sent Items</span>
<div class="_n_24 ms-bg-color-neutralLighter"> <span autoid="_n_T" class="ms-font-m _n_Z3 ms-fwt-sb ms-fcl-tp" aria-hidden="true"></span> </div>
<span class="ms-font-s ms-fcl-ns" aria-hidden="true" aria-expanded="false" aria-haspopup="true" tabindex="-1"> </span> <button autoid="_n_U" type="button" class="_n_34 ms-fwt-r ms-fcl-ns o365button hidden" style="display: none;" tabindex="-1"></button> <span style="display: none;" aria-hidden="true"></span>
</div>
<div class="_n_14 hidden"><button autoid="_n_V" type="button" class="_n_04 firefoxFavorite o365button" title="Remove from Favorites" aria-labelledby="_ariaId_69"><span class="_fc_3 owaimg ms-Icon--star ms-icon-font-size-18 ms-fcl-ns-b"> </span><span class="_fc_4 o365buttonLabel _fc_2" id="_ariaId_69" style="display: none;"></span></button></div>
</div>
</div>
</div>
</div>
Assuming you're trying to find the SENT ITEMS's - web element.
The SENT ITEM link does not seem to be the visible area [when you have more sub folders inside Inbox, this usually happens] and hence you first make a scroll to view to the element before performing anything on the element.
Here are the options to bring the element to visible area
Try with following xpath
//span[#title='Sent Items']

Upload a photo in selenium python when file path input is not visible

This is a html code of upload a photo:
<div id="choose-photo" class="controls avatar-settings inline-upload-avatar dropdown center">
<div class="uploader-image uploader-avatar clearfix">
<div class="dropdown-menu">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<ul tabindex="-1" role="menu" aria-hidden="true">
<li id="photo-choose-existing" class="photo-choose-existing upload-photo" role="presentation">
<button type="button" class="dropdown-link" role="menuitem">Prześlij zdjęcie</button>
<div class="photo-selector">
<button class="btn" type="button">
Zmień zdjęcie
</button>
<span class="photo-file-name">Nie wybrano pliku</span>
<div class="image-selector">
<input type="hidden" name="media_file_name" class="file-name">
<input type="hidden" name="media_data_empty" class="file-data">
<label class="t1-label">
<span class="u-hiddenVisually">Dodaj zdjęcie</span>
<input type="file" name="media_empty" class="file-input js-tooltip" tabindex="-1" accept="image/gif,image/jpeg,image/jpg,image/png" data-original-title="Dodaj zdjęcie">
</label>
</div>
</div>
</li>
<li id="photo-choose-webcam" class="u-hidden" role="presentation">
<button type="button" class="dropdown-link">Zrób zdjęcie</button>
</li>
<li id="photo-delete-image" class="u-hidden" role="presentation">
<button type="button" class="dropdown-link" role="menuitem">Usuń</button>
</li>
<li class="dropdown-divider" role="presentation"></li>
<li class="cancel-options" role="presentation">
<button type="button" class="dropdown-link" role="menuitem">Anuluj</button>
</li>
</ul>
</div>
</div>
</div>
I've created a simple method to send text to input (it's not visible on screen):
fileInput = driver.find_element_by_name('media_empty')
fileInput.send_keys(path)
But it doesn't do anything. Also I'm getting not any errors.
So, here's a second method, which may work:
<div class="ProfileAvatarEditing-buttonContainer">
<button class="ProfileAvatarEditing-button u-boxShadowInsetUserColorHover" type="button" tabindex="2">
<div class="ProfileAvatarEditing-addAvatarHelp">
<span class="Icon Icon--cameraPlus"></span>
<p>Dodaj zdjęcie profilowe</p>
</div>
<div class="ProfileAvatarEditing-changeAvatarHelp">
<span class="Icon Icon--camera"></span>
<p>Zmień zdjęcie profilowe</p>
</div>
<div class="ProfileAvatarEditing-dropAvatarHelp">
<span class="Icon Icon--cameraPlus"></span>
<p>Upuść zdięcie profilowe tutaj</p>
</div>
</button>
Here user can drap and drop file. I've found this question: Selenium: Drag and Drop from file system to webdriver? however I still don't know how can I use it in this situation.
So the question is how to send file path to the input to trigger file upload. In this case when you choose a file from file dialog or drag and drop it you'll see confirm window with preview on your photo. So then all what's left to do is to click confirm. But I don't know how to send it in the first place.
Any help will be appreciated.
edit:
I've found a solution (my own answer below):
fileInput = driver.find_element_by_xpath('//*[#id="photo-choose-existing"]/div/div/label/input')
fileInput.send_keys(path)
but there's one more problem: photo is uploaded but file dialog still opens - I don't know how to close it. I tried accesing it:
dialog = driver.switch_to.active_element['value']
but I don't know how to close it.
Strangely enough I found send_keys do indeed work. When I inspected html code in different browser it wasn't "media_empty" anymore, but a different name ("media[]" or something similar). Instead I've used xpath and I was stunned that it actually worked:
fileInput = driver.find_element_by_xpath('//*[#id="photo-choose-existing"]/div/div/label/input')
fileInput.send_keys(path)
try using below code:
fileInput = driver.find_element_by_css_selector("div.image-selector label.t1-label input")
driver.execute_script("arguments[0].setAttribute('value', 'YOUR_PATH_HERE')",fileInput)
Assuming that element is present on page if not explicitly wait for element to exist on page.
then try this:
driver.execute_script("document.getElementById('ID_HERE').setAttribute('value', 'PATH_HERE')");
hope this will help you!

how to locate an element inside expanded list

I have a little problem. I am writing an automatic test, that has to go to webpage, enter some keys to search, then go to advanced search options, choose one from a list and search. The point is, that I have absolutely no idea on how to locate element on a list. It looks like this: http://www.e-podroznik.pl/public/searcherFinal.do. If you click on "więcej opcji" you'll se a list of options about journey. My question: how to locate element on this expanded "więcej opcji"-list using python selenium? I have tried everything, from find_element_by* to execute_script; nothing works. I get errors like 'unable to locate element', 'compound not permitted' etc.
The HTML:
<div class="advanced-searcher dNone more-options" style="display: block;">
<label class="lblDepartureArrival"></label>
<label class="lblRadio frmButtonFlat lblDeparture"></label>
<label class="lblRadio frmButtonFlat lblDeparture"></label>
<span class="lblOmmit"></span>
<label class="lblCaptionCarrierType"></label>
<label class="lblCarrierType lblCheckbox frmButtonFlat carrierType-bus"></label>
<label class="lblCarrierType lblCheckbox frmButtonFlat carrierType-rail"></label>
<label class="lblCarrierType lblCheckbox frmButtonFlat carrierType-aut"></label>
<label class="lblCarrierType lblCheckbox frmButtonFlat carrierType-city"></label>
<label class="lblCaptionJourneyMode"></label>
<span class=""></span>
<span class=""></span>
<span class=""></span>
<label class="lblSort">
<span class="fldCaption">
Sortuj według:
</span>
<span class="fldContainer">
<select id="sortTypeV_1399324651976" name="formCompositeSearchingResults.formCompositeSearcherFinalH.sortTypeV" style="display: none;">
<option selected="selected" value="departure"></option>
<option value="time"></option>
<option value="price"></option>
</select>
<span class="frmDropdown"></span>
</span>
</label>
<a class="lnkSwitchSearcherType" title="Wyszukiwarka zaawansowana" href="/public/searcher.do?method=task&advanced=false"></a>
</div>
I specifically need to expand list:
"<select id="sortTypeV_1399324651976" name="formCompositeSearchingResults.formCompositeSearcherFinalH.sortTypeV" style="display: none;">" and then click "<option value="time"></option>
This doesn't work:
def do_the_advanced_search(self):
self.driver.find_element_by_xpath('//*[#id="frm_1399323992507"]/fieldset/div/div[2]/label[3]').click()
time.sleep(3)
neither this:
self.driver.execute_script("document.getElementById('sortTypeV_1399324651976').style=='display: inline block';")
Thanks for any help.
To expand the part that contains the dropdown, you can use this:
self.driver.find_element_by_css_selector("a>span.imageSprite.sprite-label-left").click()
Then you can use execute_script to set the value:
self.driver.execute_script("document.getElementsByClassName('current').value = 'Najkrótszy czas';")
The select itself is hidden via style=display:none; so it cannot be manipulated directly.

Categories

Resources