Why doesn't Selenium find element by name? (Python, Firefox) - python

I have the following HTML
<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">
and the following Python
btn = driver.find_element_by_name('_eventId_confirmed')
Running this code returns an error
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="_eventId_confirmed"]
Just before this non-working HTML/code-combination I have the following HTML element:
<input name="registration" id="registration" class="size-28" maxlength="7" autofocus="" autocomplete="off" type="text" data-original-title="" title="" style="">
which I successfully access with
elem = driver.find_element_by_name("registration")
Why does the second one work but not the first one?
Edit: the problem was that a new window was opened and I needed to switch window handler. So, false alarm. Thank you all.

A "tag name" is not the same as a "name". The former refers to the HTML element's tag name, while the later refers to the HTML element's "name" attribute.
For example, in your first HTML snippet,
<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">
button is the tag name while _eventId_confirmed is the (attribute) name.

This could be because of the modal dialogue, as you mentioned in a comment. Try
driver.switchTo().frame("ModalFrameTitle");
or
driver.switchTo().activeElement()

You can do it by using window_handles and switch_to_window method.
Before clicking the button the window handle as
window_before = driver.window_handles[0]
elem = driver.find_element_by_name("registration")
after clicking the button the window handle of newly opened window as
window_after = driver.window_handles[1]
then execute the switch to window methow to move to newly opened window
driver.switch_to.window(window_after)
driver.find_element_by_name("_eventId_confirmed").click()
Hope this help.

Related

Unable to click on an ahref link using Python Selenium

This is the html code:
<div id="dcf-page-export-buttons" class="no-print" style="display: block;">
<a id="dcf-saveaspdf" href="#" target="_blank" class="k-button">
Save as PDF
</a>
<a id="dcf-saveaspng" href="#" target="_blank" class="k-button">
Save as Image
</a>
<a id="dcf-printPdf" class="k-button" href="#">
Print
</a>
<a id="dcf-btnClose" class="k-button" href="#">
Close
</a>
</div>
I want to click on the Print href but it isn't being clicked. Here is my code:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
print = driver.find_element_by_id("dcf-printPdf")
print.click()
Before finding element by id for print, I had clicked on Export href which opened a new tab and after opening of the new tab, I'm trying to click on print but getting an error. This is the error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Let me know if I'm going wrong somewhere or if there is a problem in the html.
The first part of the question has been answered. This is the 2nd part:
On clicking on this Print button:
This window gets opened. It's not a new tab per se, but just a new window. Within that window, I want to click on the Save button. Is there a way to go about doing that? This is how the view looks like:
And here is the html code.
<cr-button class="action-button" aria-disabled="false" role="button" tabindex="0">
Save
</cr-button>
Here is my code till now:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
driver.switch_to.window(driver.window_handles[1])
driver.execute_script("document.getElementById('dcf-user-info').style.display = 'none';")
time.sleep(1)
print = driver.find_element_by_link_text("Print")
print.click()
This is the snip of the error log. I've added the snip because I'm unsure of the error.
Small continuation of the error:
You need to switch to new tab first then you can click on Print using that Id that you have been trying.
Switch to new windows like this :
driver.switch_to.window(driver.window_handles[1])
As you can see from the error, the element you trying to access is not interactable.
So, the problem is not with HTML.
I can't see the page you working on, but the problem can be that element you trying to access is out of the view.
Try this
from selenium.webdriver.common.action_chains import ActionChains
print = driver.find_element_by_id("dcf-printPdf")
ActionChains(driver).move_to_element(print).click(button).perform()

"Element click intercepted" Error while trying to click on CheckBox

I want to click on a check box after I load up a page, but I get the error message below:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <img class="checkboximage" src="/images/nav/ns_x.gif" alt=""> is not clickable at point (843, 7). Other element would receive the click: <a class="ns-help" onclick="nlPopupHelp('EDIT_TRAN_CUSTINVC');" tabindex="0" onkeypress="(event.keyCode == 13 || event.charCode == 32) && nlPopupHelp('EDIT_TRAN_CUSTINVC');">...</a>
This is what I have right now: I've tried several other methods before this
collectionsbox = driver.find_element(By.CSS_SELECTOR,"#custbody_in_collections_fs_inp")
driver.execute_script("arguments[0].scrollIntoView(true)", collectionsbox)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='custbody_in_collections_fs']//img[#class='checkboximage']"))).click()
collectionsbox.click() #USUALLY FAILS RIGHT HERE
savebutton = driver.find_element(By.XPATH,"//input[#id='btn_multibutton_submitter']")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='btn_multibutton_submitter']"))).click()
driver.execute_script("arguments[0].scrollIntoView(true)", savebutton)
savebutton.click()
time.sleep(2)
driver.switch_to.alert.accept()
I've tried the EC method, waited several seconds, scroll down to view the element. I've even used Xpath, ID, and CSS.
<input onclick="setEventCancelBubble(event); this.isvalid=(nlapiValidateField(null,'custbody_in_collections')); if (this.isvalid) {setWindowChanged(window, true);nlapiFieldChanged(null,'custbody_in_collections');;} else if ( window.loadcomplete && !document.page_is_resetting ) {setFormValue(this, !this.checked);}" aria-labelledby="custbody_in_collections_fs_lbl" onchange="NLCheckboxOnChange(this); " onkeypress="NLCheckboxOnKeyPress(event); return true;" name="custbody_in_collections" id="custbody_in_collections_fs_inp" type="checkbox" value="T" class="checkbox" style="">
<input type="hidden" name="custbody_in_collections_send" style="">
<img class="checkboximage" src="/images/nav/ns_x.gif" alt="" style="">
All I need is click the checkbox and click save up top.
Seems like another element on the DOM is unintentionally hiding the element you are trying to click. You can try executing Javascript for the click instead. This usually resolves the issue for me.
Instead of collectionsbox.click() #USUALLY FAILS RIGHT HERE, you can replace with:
driver.execute_script("arguments[0].click();", collectionsbox)
To address element is not clickable at point (x, y) error. We can follow below approaches to resolve this issue
Solution
1. Action Class
WebElement element = driver.findElement(By.id("yourelement ID"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
2. Element not getting clicked as it is not within Viewport
use JavascriptExecutor to get element within the Viewport:
checkBox=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"yourelement Xpath")))
ActionChains(driver).move_to_element(checkBox).click(checkBox).perform()

Message - "unknown error: cannot focus element" - in python selenium driver

<div id="ccodeinput">
<input class="dropdownheader2" type="TEXT" name="CCODE" value="435435" size="14" maxlength="22">
</div>
This is the code for the Search field(see screenshot - upper right side). I am unable to pass values from a list to this search bar. But I am able to press the search button beside it.
How can I enter values in the search field?
This is what I have tried:
inputElement = chrome_driver.find_element_by_id('ccodeinput')
inputElement.send_keys(435435)
As per the HTML you have shared the <input> tag is a child of <div> tag, so to put text within the intended element you can use :
chrome_driver.find_element_by_xpath("//input[#class='dropdownheader2' and #name='CCODE']").send_keys("435435")

Upload file - Make input visible (Error: ElementNotVisibleException)

Currently I have this input
<label class="bold grey-rectangle" data-uploading="Uploading" data-completed="Upload completed">
<span>+ Add certificate</span>
<input type="file" data-validation-allowing="jpg, jpeg, pdf" data-validation="mime" name="qualification2">
<div class="spinner hidden">
<div class="spinner-inner"></div>
</div>
</label>
and I want to send a file to the same, so I'm running the following codes:
self.driver.execute_script("document.getElementsByName('qualification2')[0].style.display='block';")
self.driver.find_element_by_name("qualification2").send_keys("certificate.jpeg")
but I'm getting the following error
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
How, effectively, I can do an upload with python and selenium?
You should try removing the hide class to make the element visible:
elm = self.driver.find_element_by_name("qualification2")
self.driver.execute_script('arguments[0].removeAttribute("class");', elm)
elm.send_keys("/absolute/path/to/certificate.jpeg")
Used the link you've provided, tested it:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://your/site")
elm = driver.find_element_by_name("qualification2")
driver.execute_script('arguments[0].removeAttribute("class");', elm)
elm.send_keys("/Users/user/Downloads/test.jpg")
Produced:
Note that in Firefox you would also have to reset the margin-left style property to 0 to make the element really visible:
driver.execute_script('arguments[0].removeAttribute("class"); arguments[0].style["margin-left"] = 0;', elm)

Selenium in Python: Breaking a While Loop After an Onclick Attribute Changes

I'm looking for a way to stop a loop that continues to click a "Next Page" button until it is no longer clickable on a web-based system, which is only indicated by a change of the Onclick value from:
onclick="javascript:__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')"
to
onclick="return false;__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')"
My problem is that I currently don't have a way to get Selenium to recognize that this change has been made.
As of now, I've been able to write something like this:
while (driver.find_element_by_class_name('rgPageNext').is_enabled()) is True:
time.sleep(1)
elem = driver.find_element_by_class_name('rgPageNext').click()
However, the button is always enabled even though the button doesn't do anything after it reaches the last page, which results in Selenium continually trying to click the button.
The complete info from inspecting the element of the button is as follows:
<input type="button" name="m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20" value=" " onclick="javascript:__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')" title="Next Page" class="rgPageNext">
<input type="button" name="m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20" value=" " onclick="return false;__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')" title="Next Page" class="rgPageNext">
You can try checking the 'onclick' attribute, whether its string startswith return false; , if so you should end the loop at the time. For that, change the while loop condition to -
while not driver.find_element_by_class_name('rgPageNext').get_attribute('onclick').startsw‌​ith("return false;"):

Categories

Resources