Click button after it is visible in Selenium using python - 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()

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

Unable to check Checkbox

I am trying to check the checkbox but i am getting following error
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element: {"method":"xpath","selector":"//mat-
checkbox[#id='mat-checkbox-1']/label/div"}
I tried selecting using id, css-selector and xpath but keep on getting error.
reg_date_checkbox = self.browser.find_element_by_xpath("//matcheckbox[#id='mat-checkbox-1']/label/div")
reg_date_checkbox.location_once_scrolled_into_view
reg_date_checkbox.click()
The HTML of checkbox is
<div _ngcontent-fep-c23="" class="last-date my-12 ng-star-inserted" fxlayout="column" style="flex-direction: column; box-sizing: border-box; display: flex;">
<mat-checkbox _ngcontent-fep-c23="" class="mat-checkbox mat-accent ng-untouched ng-pristine ng-valid" formcontrolname="deadline_enabled" id="mat-checkbox-1">
<label class="mat-checkbox-layout" for="mat-checkbox-1-input">
<div class="mat-checkbox-inner-container">
<input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" id="mat-checkbox-1-input" tabindex="0" aria-checked="false">
<div class="mat-checkbox-ripple mat-ripple" matripple="">
<div class="mat-ripple-element mat-checkbox-persistent-ripple"></div>
</div>
<div class="mat-checkbox-frame"></div>
<div class="mat-checkbox-background">
<svg xml:space="preserve" class="mat-checkbox-checkmark" focusable="false" version="1.1" viewBox="0 0 24 24">
<path class="mat-checkbox-checkmark-path" d="M4.1,12.7 9,17.6 20.3,6.3" fill="none" stroke="white"></path>
</svg>
<div class="mat-checkbox-mixedmark"></div>
</div>
</div><span class="mat-checkbox-label"><span style="display:none"> </span>The event has a registration deadline</span>
</label>
</mat-checkbox>
<!---->
You are probably trying to click on the element which is NOT of type 'checkbox'. I see there is an input with type checkbox. Please try below mentioned code
reg_date_checkbox = self.browser.find_element_by_xpath("//input[#id='mat-checkbox-1-input']")
reg_date_checkbox.click()
Additionally if you wish you perform uncheck you can look for input element's aria-checked property to be true before performing a click.
You are using this xpath //matcheckbox[#id='mat-checkbox-1']/label/div , it should be //mat-checkbox[#id='mat-checkbox-1']/label/div , there's a - in between, you are missing that.
Still you can try with this css selector :
div.mat-checkbox-inner-container input[class^='mat-checkbox-input'][id^='mat-checkbox']
There may be chances that the checkbox is in iframe, if it is the case then first switch to frame and then try to click on check box.
You could try this:
element = self.browser.find_element_by_css("input#mat-checkbox-1-input")
element.click()

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

python + selenium button in div overlay clickable in testing but not when automated

<div class="t-window-content t-content" style="overflow: auto; width: 400px; height: 389.4px;">
<div id="frmSchTarget">
<form action="/Search/SearchTypePreName"
method="post" onclick="Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));"
onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, onBegin: Function.createDelegate(this, PreNameListWindow), onSuccess: Function.createDelegate(this, Form_OnSuccess) });"><input type="hidden" id="NameList"
name="NameList">
<div style="text-align: left; padding-left: 10px">
<span style="margin-right: 20px;">
<button id="TreeViewButtonCollapse" name="TreeViewButtonCollapse" onclick="ExpandCollapseTreeView('NameListTreeView', false); return false;" title="Collapse All">
<img src="/Content/Images/toggle_up.png">
</button>
<button id="TreeViewButtonExpand" name="TreeViewButtonExpand" onclick="ExpandCollapseTreeView('NameListTreeView', true); return false;" title="Expand All">
<img src="/Content/Images/toggle_down.png">
</button>
</span>
</div>
<div id="divSelectNamesArea" style="height: 325px; overflow: auto;">
"i removed a very large list from here"
</div>
<hr class="PopupLineStyle">
<div style="text-align: left; padding-left: 10px">
<span style="margin-right: 20px;"><input type="checkbox" id="Checkbox1" name="SelectAllPrenamesToggle" onclick="SelectAllOrNoneInTreeView('NameListTreeView', this.checked);">
All / None</span>
<input type="submit" class="t-button" value="Done"
onclick="GetNameListString('NameListTreeView');">
</div>
</form>
</div>
</div>
The above code has basically been the bane of my existence for about a week now. I am trying to run a webscraper and upon submitting a request a div overlay appears on the page. The code snippet above shows how the 'Done' button used to get rid of it is implemented.
In testing I am able to use
buttons = driver.find_elements_by_xpath('//input[contains(#class,"t-button")]')
to find all buttons present and then it is button [5], however once I automate this does not work.
I have also tried an implicit wait with the following xpath:
//*[#id="frmSchTarget"]/form/div[3]/input
This also is a no-go. Please help, I have been trying just about everything an searched through stackoverflow for over a week already. I just can't figure it out.
// wait 15s before click button for debug purpose, in case click button before
// it be rendered out on page
time.sleep(15)
button_done = driver.find_element_by_css_selector("#frmSchTarget input[value='Done']")
button_done.click()
if above code work, remove the time.sleep() and run again, if failed, it means
you need wait some moment before click it, please use explicit wait replace time.sleep()

Categories

Resources