DocuSign - Selenium Python - "Sign" "Initial" element - python

As a part of our web based application, Customer has to sign contract in docusign and then the initial contract gets generated.
In the document, In some places customer initial is required and some places customer signature is required(These are not in order). I was able to do the following. But I am getting "Element is not visible" message. I guess the click is moving to "Sign here" when there is "Initial here". For each contract selected this keeps changing. How do I write my code to accommodate this? Please let me know.
try:
self.driver.find_element_by_id("chkUserEsign").click()
time.sleep(5)
self.driver.find_element_by_id("ds_hldrBdy_dlgStart_startReview_btnInline").click()
except NoSuchElementException as e:
print('retry in 10s.')
time.sleep(1)
try:
self.driver.find_element_by_id("ds_hldrBdy_navnexttext_btnInline").click()
except NoSuchElementException as e:
print('retry in 9s.')
time.sleep(1)
try:
listofinitial = self.driver.find_elements_by_xpath("//input[#type='image' and #title='Initial Here']")
for i in listofinitial:
i.click()
self.driver.find_element_by_id("ds_hldrBdy_dlgAdoptSig_btnAdoptSignature_btnInline").click()
listofsign = self.driver.find_elements_by_xpath("//input[#type='image' and #title='Sign Here']")
for j in listofsign:
j.click()
except NoSuchElementException as e:
print('retry in 5s.')
time.sleep(1)

Click function will always click on an element if it able to locate it in the HTML DOM using the unique locator specified. It does not matter what is the position the element in the UI unless your are using absolute Xpath or CSS Selector.
In your case I am assuming that either 'Initial Here' or 'Sign Here' element is visible in the UI and you are trying to locate both because of which the test script is failing due to 'Element Not Visible'
There is a function 'isDisplayed' in WebDriver which checks for visibility of the element and returns a boolean, why don't you check the visibility of both you locators before performing the click action.

Related

Selenium : switch to modal window handle gives error

I have this webpage https://account.proton.me/login?language=en where I am trying to switch to modal after logging in to the page. Please note that the modal appears even if you give wrong id and password, so to reproduce you can use the same code below
driver.get('https://account.proton.me/login?language=en')
usernameField = driver.find_element(By.XPATH,'//*[#id="username"]')
usernameField.send_keys("kuchbhirandom#some.thing")
passwordField = driver.find_element(By.XPATH,'//*[#id="password"]')
passwordField.send_keys("yehbhikuchbhi")
loginbutton = driver.find_element(By.XPATH,'//button[#type="submit"]')
loginbutton.click()
The above code gives us the modal
I ttried checking the window handles and switching to them one by one which gives me
driver.window_handles
['CDwindow-34B695696D2295F87F84F06321D10117', 'CDwindow-212C47AEC0CCD8240A4F9675D5B5BEF2', 'CDwindow-5A39DFE9B9C75CFA7316BF2098765D05', 'CDwindow-796B9EF6777039A036CCF7C3D452807A', 'CDwindow-1DF355426CF56902DC339955FF55C9AE', 'CDwindow-1B700B499C716086FA269E89B0ED3835']
for handle in driver.window_handles:
driver.switch_to.window(handle)
try:
checkbox = driver.find_element(By.XPATH,'//div[#id="checkbox"]')
print('found')
except:
pass
but I get the same error "No such element"
Talking about solving the captch : I have an external API that does it for me, but I need to click that check box here, but stuck in switching to the modal part
Note that : to reproduce issue you can use same code above, no need to create account.
Problem is that you have 2 nested iframes in the site,
You'll have to perform driver.switch_to.frame() for each of them:
# After pressing "sign-in"
captcha_iframe = driver.find_element(By.CSS_SELECTOR, '[title="Captcha"]')
driver.switch_to.frame(captcha_iframe)
inner_iframe = driver.find_element(By.CSS_SELECTOR, 'iframe')
driver.switch_to.frame(inner_iframe)
# Perform captcha check
driver.find_element(By.CSS_SELECTOR, '#checkbox').click()
Notice that after each switch_to, I directly use the new driver's context, and I do not perform a search under the element I found.

How to access a element that is loaded after a click event using selenium python

The problem is that I'm trying to fill in a form for login. For the form to appear, I need first to click on a "sign in" button that I already clicked, then it shows me the form. But when I use
email_input = driver.find_element_by_xpath("//input[#name='emailAddress']")
to access the email input, it returns me the following error:
no such element: Unable to locate element: {"method":"xpath","selector":"//input[#name='emailAddress']"}
I already tried to change the xpath, to use a time.sleep for wait the input to load, and nothing solved the problem.
site: nike.com.br
code:
def login(driver, username, password): try: LOGGER.info("Requesting page: " + NIKE_HOME_URL) driver.get(NIKE_HOME_URL) except TimeoutException: wait_until_visible(driver=driver, xpath="//a[#id='anchor-acessar-unite-oauth2']") driver.find_element_by_xpath("//a[#id='anchor-acessar-unite-oauth2']").click() wait_until_visible(driver=driver, xpath="//input[#name='emailAddress']") email_input = driver.find_element_by_xpath("//input[#name='emailAddress']") email_input.clear() email_input.send_keys(username)

IF ELSE Statement in python selenium webdriver not executing Else when element is not there

I am using an automation script with selenium web driver. It usually works fine, but sometimes the content on a page is changing.
If the element it is looking for is not there, it is crashing instead of executing the else statement.
I Tried to use another function with try and NoSuchElementException, but I do get another error about NoSuchElementException.
This is the if else statement:
#Look for link text "Stores"
find_store = driver.find_element_by_link_text('Stores')
if find_store:
find_store.click()
else:
driver.get("https://example.com/myaccount")
time.sleep(5)
This is the try statement:
try:
find_store = driver.find_element_by_link_text('Stores')
if find_store.is_displayed():
find_store.click() # this will click the element if it is there
print("Store found, all good.")
except NoSuchElementException:
driver.get("https://example.com/myaccount")
print("Store was not found, visiting base URL to search for store")
time.slep(5)
In the first script I expect it to search for the element. If its not there, then visit the URL so I can search for the element there. But it never opens the URL and I get this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
In the second script I expect to look for the element. If the element is not there, it should visit the base url and try again later in the code. However here I get this error:
except NoSuchElementException:
NameError: name 'NoSuchElementException' is not defined
I would like to learn how I can avoid the script crashing and actually execute an alternative action if the element is not there. Thank you.
Use Following :
if len(driver.find_elements_by_link_text('Stores'))> 0:
driver.find_element_by_link_text('Stores').click()
else:
driver.get("https://example.com/myaccount")
time.sleep(5)

selenium: wait for element to be clickable not working

I'm attempting to use expected_conditions.element_to_be_clickable but it doesn't appear to be working. I'm still seeing "Element...is not clickable at point" errors in about 30% of the runs.
Here's the full error message:
selenium.common.exceptions.WebDriverException: Message: unknown
error: Element ... is not clickable at point (621, 337). Other
element would receive the click: ...
(Session info: chrome=60.0.3112.90)
(Driver info: chromedriver=2.26.436421 (6c1a3ab469ad86fd49c8d97ede4a6b96a49ca5f6),platform=Mac OS X 10.12.6
x86_64)
Here's the code I'm working with:
def wait_for_element_to_be_clickable(selector, timeout=10):
global driver
wd_wait = WebDriverWait(driver, timeout)
wd_wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)),
'waiting for element to be clickable ' + selector)
print ('WAITING')
return driver.find_element_by_css_selector(selector)
Update:
So now this is really odd. Even if I add a couple of fixed wait periods, it still throws the error message occasionally. Here's the code where the call is being made:
sleep(5)
elem = utils.wait_for_element_to_be_clickable('button.ant-btn-primary')
sleep(5)
elem.click()
Ended up creating my own custom function to handle the exception and perform retries:
""" custom clickable wait function that relies on exceptions. """
def custom_wait_clickable_and_click(selector, attempts=5):
count = 0
while count < attempts:
try:
wait(1)
# This will throw an exception if it times out, which is what we want.
# We only want to start trying to click it once we've confirmed that
# selenium thinks it's visible and clickable.
elem = wait_for_element_to_be_clickable(selector)
elem.click()
return elem
except WebDriverException as e:
if ('is not clickable at point' in str(e)):
print('Retrying clicking on button.')
count = count + 1
else:
raise e
raise TimeoutException('custom_wait_clickable timed out')
The problem is stated in the error message.
Element ... is not clickable at point (621, 337). Other element would receive the click: ...
The problem is that some element, the details of which you removed from the error message, is in the way... on top of the element you are trying to click. In many cases, this is some dialog or some other UI element that is in the way. How to deal with this depends on the situation. If it's a dialog that is open, close it. If it's a dialog that you closed but the code is running fast, wait for some UI element of the dialog to be invisible (dialog is closed and no longer visible) then attempt the click. Generally it's just about reading the HTML of the element that is blocking, find it in the DOM, and figure out how to wait for it to disappear, etc.

Python Selenium: Element currently not visible

I have this page. I have to click on Facebook icon. Upon doing it I m getting:
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Code is given below:
if 'log' in html.lower():
print("not logged in")
sleep(3)
#Click on Fb button
fb_element = driver.find_element_by_xpath('//a[#tooltip="Facebook"]')
fb_element.vis
fb_element.send_keys(Keys.TAB)
There is an another element with tooltip="Facebook" on the page and this element is actually invisible. Well, there are actually 10 of them:
> $x('//a[#tooltip="Facebook"]').length
10
You can find find all elements matching your locator and filter the visible one via next() and is_displayed():
facebook_links = driver.find_elements_by_xpath('//a[#tooltip="Facebook"]')
visible_link = next(link for link in facebook_links if link.is_displayed())
visible_link.click()

Categories