Select checkbox using selenium in python - 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

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')]"));

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

Using splinter to access elements in popup window

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.

Can't select textbox in selenium

I am trying to access the comment textbox in a generic huffington post artical. When I right click inspect element I get the following HTML code:
<div class="UFIInputContainer">
<div class="_1cb _5yk1">
<div class="_5yk2" tabindex="-2">
<div class="_5rp7">
with the line <div class="_1cb _5yk1"> highlighted.
from selenium import webdriver
driver = webdriver.Chrome()
'''
Just pretend that I put in some code to log in to facebook
so I can actually post a comment on huffington post
'''
driver.get.('http://www.huffingtonpost.com/entry/worst-suicide-squad-reviews_us_57a1e213e4b0693164c34744?')
'''
Just a random artical about a movie
'''
comment_box = driver.find_element_by_css_selector('._1cb._5yk1')
'''
since this is a compound class I think I should use find_by_css_selector
'''
When I run this though, I get the error message: "no such element found". I have tried other methods of trying to get a hold of the comment textbox but I get the same error message and I am at a lost of how to access it. I am hoping somebody can shed some light on this problem.
edit: This is a more complete HTML code:
<html lang="en" id="facebook" class="svg ">
<head>...</head>
<body dir="ltr" class="plugin chrome webkit win x1 Locale_en_US">
<div class="_li">
<div class="pluginSkinLight pluginFontHelvetica">
<div id="u_0_0">
<div data-reactroot class="_56q9">
<div class="_2pi8">
<div class="_491z clearfix">...</div>
<div spacing="medium" class="_4uyl _1zz8 _2392 clearfix" direction="left">
<div class="_ohe lfloat">...</div>
<div class>
<div class="UFIInputContainer">
<div class="_1cb _5yk1">
<div class="_5yk2" tabindex="-2">
<div class="_5rp7">
</div>
</div>
<div class="UFICommentAttachmentButtons clearfix">...</div>
<!-- react-empty: 39 -->
<div class="_4uym">...</div>
</div>
</div>
</div>
::after
You have to switch to the iframe containing the text box. Try the following approach, it should work:
Clicking the load comments button might be required first if load comment button is displayed
load_comment = driver.find_element_by_css_selector('.comment-button.js-comment-button')
load_comment.click()
driver.switch_to_frame(driver.find_element_by_css_selector('.fb_ltr.fb_iframe_widget_lift'))
comment_box = driver.find_element_by_css_selector('._1cb._5yk1')
comment_box.send_keys('Test')

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