Using splinter to access elements in popup window - python

On clicking a button on a webpage, a new window opens which on 'inspect element' looks like this:
<div id="editPhoneModal" class="modal hide" tabindex="-1" role="dialog" aria-labelledby="editPhoneLabel"
aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="editPhoneLabel">Edit Phone Properties</h3>
</div>
<div class="modal-body">
</div>
When i use find_by_xpath to find elements () seen after this window opens, it says 'element not found'
How can I use splinter to access the elements seen AFTER this window appears? Below is my code:
from splinter import Browser
driver = Browser('firefox')
driver.visit('http://website.com')
driver.find_by_id('user').fill('myname')
driver.find_by_id('password').fill('123456')
driver.find_by_text('Sign in').first.click()
driver.find_by_xpath("/html/body/div[2]/div[1]/div[2]/table/tbody/tr[6]/td[1]/i").first.click()#pop-up window appears
driver.find_by_xpath("//*[#class='select2-input']").fill('newphone')
I have seen solutions with selenium, but nothing with splinter.

Related

How i can click on a div with role button without text? Using Python Selenium

This is the html code exemple:
<div aria-label="Continue" class="my-class" data-visualcompletion="ignore"></div>
<div class="div1-class">
<div class="div1-class2">
<span class="area-span" dir="auto">
<span class="text-span">Continue</span>
</span>
</div>
</div>
<div class="div2-class" data-visualcompletion="ignore"></div>
I'm trying:
continue = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'my-class')) )
continue.click()
but it doesn't work in any of the ways I tried.
You should check the xpaths in the browser console.
And try doing this ?
driver.findElement(By.xpath("//div[contains(#class,'my-class')]"));

Select checkbox using selenium in python

I want to select a checkbox using selenium in python. Following is the HTML of the checkbox. The span element is getting highlighted when hovering the mouse over checkbox
HTML
<div id="rc-anchor-container" class="rc-anchor rc-anchor-normal rc-anchor-light">
<div id="recaptcha-accessible-status" class="rc-anchor-aria-status" aria-hidden="true">Recaptcha requires verification. </div>
<div class="rc-anchor-error-msg-container" style="display:none"><span class="rc-anchor-error-msg" aria-hidden="true"></span></div>
<div class="rc-anchor-content">
<div class="rc-inline-block">
<div class="rc-anchor-center-container">
<div class="rc-anchor-center-item rc-anchor-checkbox-holder"><span class="recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox" role="checkbox" aria-checked="false" id="recaptcha-anchor" tabindex="0" dir="ltr" aria-labelledby="recaptcha-anchor-label"><div class="recaptcha-checkbox-border" role="presentation"></div><div class="recaptcha-checkbox-borderAnimation" role="presentation"></div><div class="recaptcha-checkbox-spinner" role="presentation"></div><div class="recaptcha-checkbox-spinnerAnimation" role="presentation"></div><div class="recaptcha-checkbox-checkmark" role="presentation"></div></span></div>
</div>
</div>
<div class="rc-inline-block">
<div class="rc-anchor-center-container">
<label class="rc-anchor-center-item rc-anchor-checkbox-label" aria-hidden="true" role="presentation" id="recaptcha-anchor-label"><span aria-live="polite" aria-labelledby="recaptcha-accessible-status"></span>I'm not a robot</label>
</div>
</div>
</div>
<div class="rc-anchor-normal-footer">
<div class="rc-anchor-logo-portrait" aria-hidden="true" role="presentation">
<div class="rc-anchor-logo-img rc-anchor-logo-img-portrait"></div>
<div class="rc-anchor-logo-text">reCAPTCHA</div>
</div>
<div class="rc-anchor-pt">Privacy<span aria-hidden="true" role="presentation"> - </span>Terms</div>
</div>
</div>
I am trying the following code but it is giving following exception selenium.common.exceptions.NoSuchElementException
My Code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = 'C:\Program Files (x86)\Google\Chrome\chromedriver'
browser = webdriver.Chrome(chromedriver)
browser.get(url)
checkBox = browser.find_element_by_id("recaptcha-anchor")
checkBox.click()
This is a recaptha stuff .. it's not like normal elements in the page
you have to navigate with selenium to the captcha frame .. then you can deal with the checkbox element..
to do that you need first to save the main window handle to be get back to it when you're done with the recaptcha
# save the main window handle
mainwindow = browser.current_window_handle
# get the recapthca iframe then navigate to it
frame = browser.find_element_by_tag_name("iframe")
browser.switch_to.frame(frame)
# now you can access the checkbox element
browser.find_element_by_id("recaptcha-anchor").click()
# navigate back to main window
browser.switch_to.window(mainwindow)
for further info about how to deal with the recaptcha challenge check this link

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

Click button after it is visible in Selenium using python

I'm trying to emulate a search using Python and Selenium, but recently I faced a problem, when a button is generated but a JS. Here, in the picture below, the button Im t rying to click is "Save..." and it is visible (and clickable) after search is finished. Using code analyzer in Firefox, I found that this button's code is:
<div class="pharmit_minbottom">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button">
<span class="ui-button-text-only">
Save...
</span>
</button>
</div>
But in this code there are two more buttons with the same text label:
<div class="pharmit_resfooter">
<div class="pharmit_bottomloaders pharmit_nowrap">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-disabled ui-state-disabled ui-button-text-only" disabled="" role="button">
<span class="ui-button-text">
Minimize
</span>
</button>
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-disabled ui-state-disabled ui-button-text-only" disabled="" role="button">
<span class="ui-button-text">
Save...
</span>
</button>
</div>
</div>
I tried to click it using two different pieces of code:
time.sleep(30) #wait for the search to finish
driver.find_element_by_class_name('ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only').click()
And another one:
wait = WebDriverWait(driver, 30)
wait = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only')))
wait.click()
But neither of them seems to work. how can I overcome this situation and finally click this button?
try the following code:
Using WebDriverWait: (helps to avoid sleep in case of slow loading)
wait = WebDriverWait(driver, 30)
saveButton = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='pharmit_‌​bottomloaders pharmit_nowrap']/button[2]/span")))
saveButton.click()
Without WebDriverWait:
driver.find_element_by_xpath("//div[#class='pharmit_‌​bottomloaders pharmit_nowrap']/button[2]/span").click()

How to select a label text in browser pop in selenium 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'.

Categories

Resources