Can't Click an Element in Python Selenium After Successfully Finding It - python

I am using the following script to find elements called 'service_notes' and click the first of them:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import os
driver = webdriver.Firefox(executable_path=r'geckodriver.exe')
driver.get("https://.../sites/frontiersupport/servicenotes/Pages/default.aspx")
os.system("java -jar sikulix.jar -r login.sikuli")
driver.implicitly_wait(5) #
service_notes = driver.find_elements(By.XPATH, '//*/a[starts-with(#href, "/sites/frontiersupport/servicenotes/Lists/SNotes/DispForm.aspx")]')
print(str(service_notes[0]) + ' is the first service note')
print(len(service_notes))
service_notes[0].click()
According to the log, the element is found, but for some reason I can't click it:
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="7505cfd0-5f41-4a8c-af28-bca639e13332", element="c81518a8-5f6c-42fa-8089-5f9469423d7c")> is the first service note
10
Traceback (most recent call last):
File "scraper.py", line 19, in <module>
service_notes[0].click()
File "C:\Users\Joseph\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Joseph\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\Joseph\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\Joseph\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: No modal dialog is currently open
The browser is defniitely open, and I'm not sure why the driver is looking for an open 'modal dialog'. This should simply be clicking a link. Any ideas?
Here's the HTML of the link I want to click:
<a onfocus="OnLink(this)" href="/sites/frontiersupport/servicenotes/Lists/SNotes/DispForm.aspx?ID=14686" onclick="GoToLink(this);return false;" target="_self">Firmware 3.04 released for the 850-DS <img src="/_layouts/images/blank.gif" class="ms-hidden" alt="Use SHIFT+ENTER to open the menu (new window)." width="1" height="1" border="0"></a>

I've encountered this problem of not being able to click on elements too. One way I found to go around it was to use execute_script method. In your case -
driver.execute_script("arguments[0].click();", service_notes[0])

Related

"Element <> could not be scrolled into view" error not responding to usual solutions

I'm using selenium to design a scraper to mass-download podcast episodes from Spreaker.
# https://www.spreaker.com/show/alabamas-morning-news-with-jt
for i in range(3):
print("Click number: {}".format(str(i)))
see_more = browser.find_element_by_id("show episodes more")
see_more.click()
browserPage = bs4(browser.page_source, 'lxml')
allEps.append( allEpisodesOnPage(browserPage) )
Since they aren't all on numbered pages (/episodes/page1, /page2), I have to click a button to load more.
But for some reason, my code can't find the button:
Traceback (most recent call last):
File "KeepTalking__02.py", line 59, in <module>
see_more.click()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <a id="show_episodes_more" class="btnn btnn_alternative btnn_small" href="#"> could not be scrolled into view
Now, "could not be scrolled into view" is a standard error in Selenium, and I was inundated with possible solutions:
Scroll the element into view.
Wait for the element to become visible:
element = WebDriverWait(browser, 10).until(
ex_co.presence_of_element_located((By.CSS_SELECTOR, "#show_episodes_more")))
Switch to the frame that it's on.
But for some reason, I'm still getting the exact same error. What could cause this? I took a screenshot and the button was on the page, so I don't understand where the error is coming from.
Basically ElementNotInteractableException is occurs when emporary Overlay of other WebElement over the WebElement. To avoid ElementNotInteractableException you can use ActionChains please refer below solution :
element = WebDriverWait(browser, 10).until(
ex_co.presence_of_element_located((By.CSS_SELECTOR, "#show_episodes_more")))
ActionChains(driver).move_to_element(element).click().perform()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
or
element= wait(self.browser,30).until(EC.element_to_be_clickable((By.XPATH,"//a[#id='show_episodes_more']")))
driver.execute_script("arguments[0].click();", element)

How to find and click on the element to login using Selenium and Python

I couldn't find and click the element. HTML is as follows:
<button _ngcontent-c2=""> INICIAR SESIÓN </button>
I tried using the code:
login_element = driver.find_element_by_xpath('/html/body/app-root/div/div/app-login/form/div/div/button').click()
This is the error i got:
Traceback (most recent call last):
File "/home/eitan/PycharmProjects/pysel/autopro.py", line 36, in <module>
login_element = driver.find_element_by_xpath('/html/body/app-root/div/div/app-login/form/div/div/button').click()
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button _ngcontent-c2="">...</button> is not clickable at point (624, 648). Other element would receive the click: <img _ngcontent-c2="" src="assets/static/images/login.svg">
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Linux 4.15.0-47-generic x86_64)
Try the following xpath
login_element = driver.find_element_by_xpath("//button[contains(.,'INICIAR')]"
login_element.click()
EDITED
Seems like webdriver unable to click on the button element.inject java scripts executor to click on the button element or use action class to click.
login_element = driver.find_element_by_xpath("//button[contains(.,'INICIAR SESIÓN')]")
driver.execute_script("arguments[0].click();",login_element)
OR Action class.
login_element = driver.find_element_by_xpath("//button[contains(.,'INICIAR SESIÓN')]")
ActionChains(driver).move_to_element(login_element).click().perform()
Use the following imports for action class.
from selenium.webdriver.common.action_chains import ActionChains
As the element is an Angular element, to click() on the element with text as INICIAR SESIÓN you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
driver.execute_script("arguments[0].click();",WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'INICIAR SESIÓN')]"))))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Login script in popup window. No such element

I'm trying to get a login script to select a user name input to enter in my user name. After this popup is done there will be another one asking for the password. I'm new to python and web interfaces so I'm having trouble identifying what element of the website I need to select to get this to work. Here is the code I have so far.
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
DynamoForum = webdriver.Chrome()
DynamoForum.get("https://forum.dynamobim.com/")
login = DynamoForum.find_element_by_class_name("header-buttons").click()
#DynamoForum.switch_to_frame(DynamoForum.find_element_by_
#wait(DynamoForum,10).until(EC.frame_to_be_available_and_switch_to_it(
DynamoForum.find_element_by_xpath("//title[1]")))
wait(DynamoForum,10).until(EC.frame_to_be_available_and_switch_to_it(
DynamoForum.find_element_by_xpath(
"//iframe[#id='destination_publishing_iframe_autodesk_0']")))
DynamoForum.find_element_by_id("userName").send_heys("xxx")
The website is opening and the popup is starting but no text is being entered. Here is what my getting as a result:
Traceback (most recent call last):
File "C:/Users/cjr/PycharmProjects/DynamoForum/DynamoForum.py", line 17, in <module>
wait(DynamoForum, 10).until(EC.frame_to_be_available_and_switch_to_it(DynamoForum.find_element_by_xpath("//iframe[#id='destination_publishing_iframe_autodesk_0']")))
File "C:\Users\cjr\PycharmProjects\DynamoForum\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\cjr\PycharmProjects\DynamoForum\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\cjr\PycharmProjects\DynamoForum\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\cjr\PycharmProjects\DynamoForum\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[#id='destination_publishing_iframe_autodesk_0']"}
(Session info: chrome=72.0.3626.119)
(Driver info: chromedriver=73.0.3683.20 (8e2b610813e167eee3619ac4ce6e42e3ec622017),platform=Windows NT 10.0.17134 x86_64)
Basically when you are clicking on login button, you are moving to another window and to access the element in new window you need to switch it from parent window to access this.Try the below code it should work.
from selenium import webdriver
DynamoForum = webdriver.Chrome()
DynamoForum.get("https://forum.dynamobim.com/")
Parent_window = DynamoForum.window_handles[0]
login = DynamoForum.find_element_by_class_name("header-buttons").click()
window_child= DynamoForum.window_handles[1]
DynamoForum.switch_to.window(window_child)
DynamoForum.find_element_by_id("userName").send_keys("xyz#gmail.com")
DynamoForum.find_element_by_id("verify_user_btn").click()
wait=WebDriverWait(DynamoForum,20)
wait.until(EC.visibility_of_element_located((By.ID,"password"))).send_keys("xxx")
DynamoForum.find_element_by_id("btnSubmit").click()
You need to switch to the iframe.
e.g.
iframe = driver.find_element_by_id('destination_publishing_iframe_autodesk_0')
driver.switch_to.frame(iframe)
driver.find_element_by_name('userName').send_keys('xxx')
See the switch_to function here : https://selenium-python.readthedocs.io/api.html?highlight=iframe
For reference:
python selenium cant find iframe xpath
https://seleniumwithjavapython.wordpress.com/selenium-with-python/intermediate-topics/handling-iframes-in-a-webpage/

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable using Selenium

I am searching to do a program for fun but i have some problems with selenium and i
need some help...
This is the programm (i deleted the directory of webdriver because the folder's name contain the name of an other person)
from selenium import webdriver
import webbrowser
import time
def Pass_send_():
driver=webdriver.Chrome()
driver.get('chrome://flags/#password_export-enable')
ricerca=driver.find_element_by_id("search")
ricerca.send_keys('password export')
scorritore=driver.find_element_by_class_name('experiment-select')
scorritore.click()
Pass_send_()
And so the purpose it's easy, it should open a windows, type a text and click a button. everything works but the click doesn't and this is the error:
Traceback (most recent call last):
File "C:\Python34\internet22.py", line 18, in <module>
Pass_send_()
File "C:\Python34\internet22.py", line 14, in Pass_send_
scorritore.click()
File "C:\Python34\lib\site-
packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python34\lib\site-
packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 242, in
check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
So i am not an expert but it says: element not intercatable? what does it mean and how can i fix it? i would really appreciate a reply...
To send a character sequence to the search box within the webpage chrome://flags/#password_export-enable you need to induce WebDriverWait and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('chrome://flags/#password_export-enable')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys("password export")
Browser Snapshot:

Exception on execution PhantomJS + Selenium Webdriver + Python program

With all installation prerequisite of PhantomJS and Selenium on my Ubuntu machine I am running below code snippet:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()
On execution I am getting below error:
$ python duck.py
Traceback (most recent call last):
File "duck.py", line 5, in <module>
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 208, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 664, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Error Message => 'Unable to find element with id 'search_form_input_homepage''
caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"107","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:50789","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"26560250-fec9-11e4-b2ee-2dada5838664\", \"value\": \"search_form_input_homepage\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/26560250-fec9-11e4-b2ee-2dada5838664/element"}
Screenshot: available via screen
Setting --ssl-protocol=any service argument and using Explicit Waits made it work for me:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.PhantomJS(service_args=['--ssl-protocol=any'])
driver.maximize_window()
driver.get("https://duckduckgo.com/")
wait = WebDriverWait(driver, 10)
search = wait.until(EC.presence_of_element_located((By.ID, "search_form_input_homepage")))
search.send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()
Prints https://duckduckgo.com/?q=realpython.
Note that without --ssl-protocol=any PhantomJS hasn't even loaded the page and the current url stayed as about:blank.
I think whats happening to you is that you try to find an element which is not loaded yet on the page. So what I can recommend is the insert a WaitForElementDisplayed(by.ID("search_form_input_homepage")); right before you try to type in the search field. So you will be sure that the element is there before trying to interact with it.
I can't really give you a code example because I'm not really familiar with the Python bindings.

Categories