Selenium Webdriver :- Element Not Interactable Exception for the search option - python

I am trying to fill in the search input type box with value "fab" and then I want to display the next url with that keyword but I am getting this error for element not interactable. How can I solve this?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='C:\\Users\\Mansi Dhingra\\Downloads\\chromedriver')
driver.get("https://www.thenational.ae/search?q=")
print(driver.title)
driver.implicitly_wait(10)
search_bar = driver.find_element_by_xpath('//input[#name="q"]')
print(search_bar)
search_bar.clear()
search_bar.send_keys("fab")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
Error:-
Traceback (most recent call last): File "C:/Users/Mansi
Dhingra/Desktop/Projects/api/news/news_python.py", line 10, in
search_bar.clear() File "C:\Users\Mansi Dhingra\Desktop\Projects\api\venv\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 95, in clear
self._execute(Command.CLEAR_ELEMENT) File "C:\Users\Mansi Dhingra\Desktop\Projects\api\venv\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 633, in _execute
return self._parent.execute(command, params) File "C:\Users\Mansi
Dhingra\Desktop\Projects\api\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 321, in execute
self.error_handler.check_response(response) File "C:\Users\Mansi Dhingra\Desktop\Projects\api\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotInteractableException: Message:
element not interactable (Session info: chrome=81.0.4044.92)

Whenever webelement is found but not ready to interact then webdriver will throw element not intractable exception.
1.Mostly it occurs, when element is located in bottom of the page, so it can be accessible by scrolling the page down. You can use Action class to scroll to scroll to that element
WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
## actions.click();
actions.perform();
2. Sometimes we need to wait few seconds to access the web element, in such situations we can add wait statements.
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

Related

Unable to locate element, can't scrape 'reviews'

I'm scraping the product reviews from the sephora website which contains javascript(reviews), but I can't scrape. This is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located as EC
import time
chrome_path = '/media/danish-khan/New Volume/Web_scraping/rgcrawler2/chromedriver'
driver = webdriver.Chrome(chrome_path)
chrome_options = Options()
url = 'https://www.sephora.com/product/the-porefessional-face-primer-P264900?skuId=1259068&icid2=products%20grid:p264900:product'
driver.get(url)
WebDriverWait(driver, 70)
time.sleep(70)
review = driver.find_element_by_class_name('css-1jg2pb9 eanm77i0')
for post in review:
#try:
# element = WebDriverWait(driver, 50).until(
# EC.presence_of_element_located((By.XPATH, "//div[#class = 'css-1jg2pb9 eanm77i0']"))
# )
#finally:
# driver.quit()
#
print(review)
driver.close()'
The output is:
Traceback (most recent call last):
File "resgt.py", line 15, in
review = driver.find_element_by_class_name('css-1jg2pb9 eanm77i0')
File "/home/danish-khan/miniconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/home/danish-khan/miniconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/home/danish-khan/miniconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/danish-khan/miniconda3/lib/python3.7/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":"css selector","selector":".css-1jg2pb9 eanm77i0"}
(Session info: chrome=85.0.4183.102)
The reviews for that page are being loaded in asynchronously specifically when the section is scrolled into view. you will have to scroll to an element close to where the reviews are and wait until it appears. Only then you will be able to retrieve the element.
I was able to do this with this code
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")
time.sleep(10)
review = driver.find_element_by_css_selector('.css-1jg2pb9.eanm77i0')
# review = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/main/div/div[2]/div[1]/div/div[5]/div/div[2]/div[1]/div[2]')
print(review)
I left the Xpath in there as thats what i used to get it the first time
note* you may have to adjust the timing and the scroll height to get it always correct

python with selenium automating login

I'm new to selenium
Here I want to ask about a problem code (actually not mine)
this is the code
aww = email.strip().split('|')
driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent")
time.sleep(5)
loginform = driver.find_element_by_xpath("//button[#data-provider='google']")
loginform.click()
mailform = driver.find_element_by_id('identifierId')
mailform.send_keys(aww[0])
driver.find_element_by_xpath("//div[#id='identifierNext']").click()
time.sleep(3)
passform = driver.find_element_by_css_selector("input[type='password']")
passform.send_keys(aww[1])
driver.find_element_by_id('passwordNext').click()
time.sleep(3)
driver.get("https://myaccount.google.com/lesssecureapps?pli=1")
open('LIVE.txt', 'a+').write(f"CHECKED : {aww[0]}|{aww[1]}")
time.sleep(3)
lessoff = driver.find_element_by_xpath('//div[#class="hyMrOd "]/div/div/div//div[#class="N9Ni5"]').click()
driver.delete_all_cookies()
driver.close()
I'm using those code for automating turn on the less-secure apps from Gmail
and the error will pop up like this
quote Traceback (most recent call last):
File "C:\Users\ASUS\Downloads\ok\less.py", line 59, in
lessoff = driver.find_element_by_xpath('//div[#class="hyMrOd "]/div/div/div//div[#class="N9Ni5"]').click()
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\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\ASUS\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\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":"//div[#class="hyMrOd "]/div/div/div//div[#class="N9Ni5"]"}
(Session info: chrome=86.0.4240.183)
any help gonna be helpfull,sorry for my english before :)
You can simply target this xpath and .click to toggle the less secure apps.
lessoff = driver.find_element_by_xpath("input[type='checkbox']").click()
It looks like it couldn't find the element it's looking for so give some time to load the element. You can check with Wait().until.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 'YOUR_XPATH')))
when you try to click an element make sure it's there. above code will wait until the element located for the 10s if the element not located then it will throw an exception

selenium wedriver program can not go on

I am trying to use webdriver to click the login button and the page has transformed correctly.but the program stop and occured the proplem "selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable"
and this is the code where occured problems
browser.find_element_by_xpath(
'//*[#id="emap-rsids-content"]/div/div[3]/div/div[1]/div/div/div/input').send_keys(uid)
browser.find_element_by_xpath(
'//*[#id="emap-rsids-content"]/div/div[3]/div/div[2]/div/div/div/input').send_keys(pwd)
# click to sign in
browser.find_element_by_xpath('//*[#id="emap-rsids-content"]/div/div[3]/div/div[3]/div/button').send_keys(Keys.ENTER)
time.sleep(3)
browser.find_element_by_xpath('/html/body/main/article/section[1]/div/div/div/div[2]/div/div/div[2]/div[2]').click()
this is the traceback
Traceback (most recent call last):
File "C:/Users/14638/Desktop/auto_sign_zzu_jksb-master/auto_sign.py", line 68, in sign_in
time.sleep(3)
File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=83.0.4103.116)
the line 68 is time.sleep(3), the click works and transform to a new page,but it still occured that the button element is interctable.
I have try two methods, one is
'''
browser.find_element_by_xpath('//*[#id="emap-rsids-content"]/div/div[3]/div/div[3]/div/button').click()
'''
and another is
'''
pages=browser.find_element_by_xpath('//*[#id="emap-rsids-content"]/div/div[3]/div/div[3]/div/button')
browser.execute_script("arguments[0].click();", pages)
'''
but still not work
I think you are trying to click the element which is not completely loaded.What you have to do is wait till that happens.
First import these files
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Then add this after logging in.
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/article/section[1]/div/div/div/div[2]/div/div/div[2]/div[2]')))
Then do
browser.find_element_by_xpath('/html/body/main/article/section[1]/div/div/div/div[2]/div/div/div[2]/div[2]').click()
You need to update the page_source:
browser.find_element_by_xpath(
'//*[#id="emap-rsids-content"]/div/div[3]/div/div[1]/div/div/div/input').send_keys(uid)
browser.find_element_by_xpath(
'//*[#id="emap-rsids-content"]/div/div[3]/div/div[2]/div/div/div/input').send_keys(pwd)
# click to sign in
time.sleep(5) # add some time to load the page
browser.get(browser.current_url)
time.sleep(1)
browser.find_element_by_xpath('//*[#id="emap-rsids-content"]/div/div[3]/div/div[3]/div/button').send_keys(Keys.ENTER)
time.sleep(3)
browser.find_element_by_xpath('/html/body/main/article/section[1]/div/div/div/div[2]/div/div/div[2]/div[2]').click()

Message: element not intractable still not working even after a time.sleep() and WebDriverWait

I know this has been asked lots of times before but how do you get around the "element not intractable" exception?
I'm pretty new to Selenium so excuse me if I get something wrong or misunderstood.
I have tried adding time.sleep(20) in various parts of the code to see if this allows the element to load but no success as yet.
Am I missing something here?
# -*- coding: utf-8 -*-
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
import time
#Login Credentials
email = 'anexample#fakeemail.com'
password = 'Password123'
#Login to Money Dashboard
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get("https://my.moneydashboard.com/")
loginPageEmail = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="input_0"]')))
loginPageEmail.send_keys(email)
I always get an error along the lines of:
Traceback (most recent call last):
File "mdash.py", line 26, in <module>
loginPageEmail.send_keys(email)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py",
line 479, in send_keys
'value': keys_to_typing(value)})
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py",
line 633, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 321, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=75.0.3770.100)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Mac
OS X 10.13.6 x86_64)
If you check the element id attribute is dynamic every time you run the code.So name attribute should be unique attribute here to access the input element .However Without form element it is not identifying the input element so I have used the form element along with input element and unique property of input element.
Use WebdriverWait and elementtobeclickable and following xpath.
email = 'anexample#fakeemail.com'
password = 'Password123'
#Login to Money Dashboard
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get("https://my.moneydashboard.com/")
loginPageEmail = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//form[#name="vm.registerForm"]//div[#class="inputs"]//input[#name="email"]')))
loginPageEmail.send_keys(email)
loginPagepassword = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//form[#name="vm.registerForm"]//div[#class="inputs"]//input[#name="password"]')))
loginPagepassword.send_keys(password)
Output:

Selenium can't click on a element in an iframe

So im trying to click a button in an iframe but it just errors saying that the button can't be found.
recaptcha = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(recaptcha)
submit1 = driver.find_element_by_id('recaptcha-anchor')
actions = ActionChains(driver)
actions.move_to_element(submit1)
actions.click(submit1).perform()
time.sleep(3)
driver.find_element_by_id('recaptcha-audio-button').click()
On the last line I try to get an element by id recaptcha-audio-button.
As I said above it just errors with that it can't find it.
The website that I am trying to do this with is: https://patrickhlauke.github.io/recaptcha/
Why can't I click this button?
Thanks, Ira.
EDIT
Here is the error
Traceback (most recent call last):
File "main.py", line 62, in <module>
driver.find_element_by_id('recaptcha-audio-button').click()
File "/home/ira/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 269, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/home/ira/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/home/ira/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/home/ira/.local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.
Element is in different iframe so you can do:
switch to main window
find all iframes and switch to second iframe
find and click your button
Code:
# go back to main window
driver.switch_to.default_content()
# find all frames and switch to second frame
all_frames = driver.find_elements_by_tag_name('iframe')
driver.switch_to.frame(all_frames[1])
driver.find_element_by_id('recaptcha-audio-button').click()
Minimal working code so everyone can copy and test it
import selenium.webdriver
import time
driver = selenium.webdriver.Firefox()
driver.get('https://patrickhlauke.github.io/recaptcha/')
recaptcha = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(recaptcha)
submit1 = driver.find_element_by_id('recaptcha-anchor')
actions = selenium.webdriver.ActionChains(driver)
actions.move_to_element(submit1)
actions.click(submit1).perform()
time.sleep(3)
# go back to main window
driver.switch_to.default_content()
# find all frames and switch to second frame
all_frames = driver.find_elements_by_tag_name('iframe')
driver.switch_to.frame(all_frames[1])
driver.find_element_by_id('recaptcha-audio-button').click()

Categories

Resources