How to select a label text in browser pop in selenium Python? - python

Can someone please help me how to select "Logitech UE 4500" in web browser popup.
PSB for HTML got through "driver.page_source".
<list id="bluetooth-paired-devices-list" role="list" tabindex="0">
<div class="spacer" style="height: 0px;">
</div>
<div role="listitem" class="deletable-item bluetooth-device" paired="paired">
<div>
<div class="bluetooth-device-label">Logitech UE 4500</div>
</div>
<button class="raw-button row-delete-button custom-appearance" tabindex="-1" title="Delete this item">
</button>
</div>
<div class="spacer" style="height: 0px;">
</div>
</list>
<div role="listitem" class="deletable-item bluetooth-device">
<div><div class="bluetooth-device-label">KKHAMPOX-MOBL2</div>

Try the below code to click on the element with text 'Logitech UE 4500':
driver.find_element_by_xpath("//div[#class='bluetooth-device-label' and contains(text(),'Logitech UE 4500')]").click()
Note: The above xpath will locate the div element with class 'bluetooth-device-label' and contains innerHTML/text as 'Logitech UE 4500'.

Related

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)

Python Selenium click on input tag by data-filter-tag value

I am trying to make selenium click on input checkbox but facing different errors.
only difference between these checkbox's is data-filter-tag value so I tried this.
driver.find_element_by_css_selector("input[data-filter-tag='am']").click()
ERROR
ElementNotVisibleException: Message: element not interactable
web page is loaded 100%
HTML CODE :
<ul>
<li class="filter-checkbox active-filter">
<div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl01$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="c"></div>
<span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">
<i class="c-icon hq-icon hq-icon-crown-dealer hq-icon-small " title=""></i>Crown dealers
</span>
</li>
<li class="filter-checkbox active-filter">
<div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl02$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="r"></div>
<span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">
<i class="c-icon hq-icon hq-icon-reseller hq-icon-small " title=""></i>Authorized dealers
</span>
</li>
<li class="filter-checkbox active-filter">
<div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl03$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="cs"></div>
<span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">
<i class="c-icon hq-icon hq-icon-servicing-workshop hq-icon-small " title=""></i>Has service
</span>
</li>
<li class="filter-checkbox active-filter">
<div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl04$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="am"></div>
<span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">
<i class="c-icon hq-icon hq-icon-category-robotic-lawn-mowers hq-icon-small " title=""></i>Automower® dealers
</span>
</li>
</ul>
</div>
Site : link
Complete code :
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome('./chromedriver/chromedriver')
### website url
driver.get('https://www.husqvarna.com/us/')
sleep(6)
driver.find_element_by_css_selector("input[data-filter-tag='am']").click()
If you select the surrounding div of a checkbox with this rather nasty CSS selector, you can at least click a checkbox without an exception.
checkbox = driver.find_element_by_css_selector("#MainContentPlaceHolder_BaseContentPlaceHolder_pmainedge2edge4_0_ctl00_ctl14_dealerFilters > section:nth-child(1) > div:nth-child(1) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(4) > div:nth-child(1)")
checkbox.click()
There is a lot of JavaScript interfering with webdriver automation. I did not find a better solution yet, but at least you know there is a way to interact with that checkbox.

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

How to find selector for "new" button? Do I need to use Switch frames?

<div id="crmMasthead" tabindex="-1">
<div id="crmTopBar" class="ms-crm-TopBarContainer ms-crm-TopBarContainerGlobal newNavBarMode">
<div id="crmAppMessageBar" class="crmAppMessageBar" style="display: none; height: 0px;">
<div id="crmRibbonManager" currentribbonelement="commandContainer15" style="height: 62px; display: block; visibility: visible;">
<div id="commandContainer15" style="display: inline;">
<ul class="ms-crm-CommandBar-Menu" role="application">
<li id="ewrb_importfile|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.ewrb_importfile.NewRecord" class="ms-crm-CommandBarItem ms-crm-CommandBar-Menu ms-crm-CommandBar-Button" tabindex="-1" title="New Create a new Import File record." command="ewrb_importfile|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid" style="white-space: pre-line; display: inline-block;">
<span class="ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered" tabindex="-1" style="max-width:200px">
<a class="ms-crm-Menu-Label" tabindex="0" onclick="return false">
<img class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" tabindex="-1" src="/_imgs/imagestrips/transparent_spacer.gif" style="vertical-align:top"/>
<span class="ms-crm-CommandBar-Menu" tabindex="-1" style="max-width:150px" command="ewrb_importfile|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid"> New </span>
<div class="ms-crm-div-NotVisible"> Create a new Import File record. </div>
</a>
</span>
</li>
The Xpath Shows me like this:
.//*[#id='ewrb_importfile|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.ewrb_importfile.NewRecord']/span/a
and If i use this, Selenium doesn't click the button
First try to remove "."(dot) from your xpath and check it if it works.
Secondly, try to write the xpath yourself. For this a node, try this one:
//a[#class="ms-crm-Menu-Label"]
You should check it if the part of html that you share is inside an iframe node or not. Otherwise, you should share more. With the current part that you shared, it is not possible to say that if it is inside an iframe or not.
Also, it can be a good idea to check the visibility of the button. The last thing: do you receive any error message. If yes, share it.

Python 3 - Selenium - scraping data from nested divs

I'm reasonably new to python and I'm trying to check which div class appears first on a page. I've done this with table rows but I can't seem to wrap my head around how to do this with divs.
What I'm trying to determine is whether the latest update is an email sent <div class="EMAIL SENT"> or a notes added <div class="Notes">. The most recent item will appear first from the top, but other actions may have taken place since then, for example, <div class="Updated">
I've not managed to write any code to do this or event get close, but in my head I imagine it to work like this.
for sub_div_classes in browser.find_element_by_class_name('cb'):
classname = ~check name of sub_div_class
if classname = "EMAIL SENT":
class_info = browser.find_element_by_class_name('plus_header_Additional_info').text
print(class_info) ¬output: EMAIL SENT :Email sent on 20-03-2016 00:22:09 by [REDACTED]
trigger_1()
if classname = "Notes":
trigger_2()
~move on to next div class in list
Below is the page code I'm trying to work with. I'd be really appreciative of any advice or assistance anyone can provide.
<div class="cb" style="margin:5px 0 0 0;">
<div class="Updated">
<div class="plus_header_Additional_info">Updated :Incident Updated on 20-03-2016 00:22:52 by User = [REDACTED]
<img src="images/minus.png" style="float:right;">
</div>
<div class="plus_content" style="display: block;" id="contentDivImg2_0">
<div>
Assigned to STRIKE1,
by User = [REDACTED].
</div>
<br>
</div>
</div>
<div class="Updated">
<div class="plus_header_Additional_info">Updated :PEND CLIENT STRIKE - 1 added on 20-03-2016 00:22:36 by [REDACTED].
<img src="images/minus.png" style="float:right;">
</div>
<div class="plus_content" style="display: block;" id="contentDivImg2_1">
<div>
</div>
<br>
</div>
</div>
<div class="EMAIL SENT">
<div class="plus_header_Additional_info">EMAIL SENT :Email sent on 20-03-2016 00:22:09 by [REDACTED]
<img src="images/minus.png" style="float:right;">
</div>
<div class="plus_content" style="display: block;" id="contentDivImg2_2">
<div>
To :- [NAME]#[DOMAIN].CO.UK Subject: Ticket - [IN-000999999] Description : Dear User,
[REDACTED]
</div>
<br>
</div>
</div>
<div class="Updated">
<div class="plus_header_Additional_info">Updated :Incident Updated on 12-03-2016 10:56:15 by User = [REDACTED]
<img src="images/minus.png" style="float:right;">
</div>
<div class="plus_content" style="display: block;" id="contentDivImg2_3">
<div>
Status:- PROGRESSING changed to PEND CLIENT,
Assigned to SOFTWARE DEPLOYED,
by User = [REDACTED].
</div>
<br>
</div>
</div>
<div class="Notes">
<div class="plus_header_Additional_info">Notes :Notes Added on 12-03-2016 10:55:53 by [REDACTED].
<img src="images/minus.png" style="float:right;">
</div>
<div class="plus_content" style="display: block;" id="contentDivImg2_4">
<div>
<textarea id="notes4" name="notes1" cols="" class="emailForm_input1" style="width: 97%; overflow: hidden; word-wrap: break-word; resize: horizontal; height: 237px;" readonly="readonly">Hello,
[REDACTED]
</textarea>
</div>
<br>
</div>
</div>
</div>
Use an or with an xpath:
.xpath("//div[#class='Notes' or #class='EMAIL SENT']")[0]
If Notes comes first you will get Notes and vice versa.
If we change a bit of your html snippet like below, adding some text to <div class="EMAIL SENT">in email and changing a later tag class to <div class="Notes">in notes:
We can see using lxml how it works:
In [13]: from lxml.etree import fromstring, HTMLParser
In [14]: xml = fromstring(html, HTMLParser())
In [15]: xml.xpath("//div[#class='Notes' or #class='EMAIL SENT']")
Out[15]: [<Element div at 0x7f96598d4ea8>, <Element div at 0x7f96598d4ef0>]
In [16]: xml.xpath("//div[#class='Notes' or #class='EMAIL SENT']")[0].text
Out[16]: 'in email\n '
In [17]: xml.xpath("//div[#class='Notes' or #class='EMAIL SENT']")[1].text
Out[17]: 'in notes\n
So with selenium you want to just find the element by xpath.

Categories

Resources