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

Why does this piece of code throw an exception selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element?
As far as I can tell, I'm choosing the right element. Googling suggested to have a .click() on element before sending keys but that didn't help either.
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://netbanking.hdfcbank.com/netbanking/")
login_wait = WebDriverWait(driver, 10)
assert "Welcome to HDFC Bank" in driver.title
frame = login_wait.until(EC.presence_of_element_located((By.NAME, 'login_page')))
driver.switch_to.frame(frame)
try:
elem = login_wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'pwd_field')))
print("Page is ready!")
elem.send_keys("123456")
elem.send_keys(Keys.RETURN)
except TimeoutException:
print("Loading took too much time!")
driver.close()

This is because what element you've located by the pwd_field class name - you've actually got a span element matching the locator. Instead, you meant to get to the password input element:
elem = login_wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'input_password')))

Related

How to loop until element clickable

i am using python selenium chrome driver and i am stuck at this.
How can i loop this code until one of the elements is clickable?
Like if its finally clickable it should get clicked and print ("clickable") and if its still not clickable it should print ("Not Clickable")
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//BUTTON[#type='submit'[text()='Zum Warenkorb hinzufügen']"))).click()
WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CLASS_NAME, "c-modal__content")))
I am not sure if your use of uppercase button is correct. Use the same syntax as in html.
One more thing: check your xpath with text():
It should be: //button[#type='submit' and text()='Zum Warenkorb hinzufügen']
Also, the general case for such loop in the case of one element is:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
wait = WebDriverWait(driver, 15)
while True:
try:
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and text()='Zum Warenkorb hinzufügen']")))
print("clickable")
element.click()
except TimeoutException:
break

Finding xpath for clicking and finding text

Error: NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//td[#class='C($primaryColor) W(51%)']"}
(Session info: chrome=77.0.3865.120)
My Code is below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/ryanyee/Desktop/Python Code/Selenium/Launch
Chrome/chromedriver')
driver.get('https://finance.yahoo.com')
search_box = driver.find_element_by_id('yfin-usr-qry')
search_box.send_keys('GOOG')
search_box.submit()
name = driver.find_element_by_xpath("//td[#class='C($primaryColor) W(51%)']").text()
My problem is that it throws an error when I try to scrape the text for name.
Also, I have trouble trying to click the button "Historical Data"
This is the website I am trying to scrape from 'https://finance.yahoo.com/quote/GOOG?p=GOOG'
Please let me know what I am doing wrong! I have been stuck for days!
Try Adding wait statement after search_box.submit()
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(driver, 10).until(
EC.presence_of_element_located(By.XPATH, "//td[#class='C($primaryColor) W(51%)']"))
You need wait after submit.
search_box.submit()
name = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[#class='C($primaryColor) W(51%)']"))).text
print(name)
#this is for click Historical Data
driver.find_element_by_link_text("Historical Data").click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Selenium + Python + onclick + NoSuchElementException

I have an "onclick" element in a webpage whose HTML reads as follows:
Text 1
I am trying to use selenium to click on this element. I tried the following code (including an implicit wait)
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(webpage);
driver.implicitly_wait(5)
driver.find_element_by_xpath("//a[#onclick='blah.submit()']").click()
However, it returned a NoSuchElementException
NoSuchElementException: Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//a[#onclick='blah.submit()']"}
I then tried using WebDriverWait
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.Chrome()
driver.get(webpage);
button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,
"//a[#onclick='blah.submit()']")))
button.click()
However, this still didn't work and returned the following error:
TimeoutException: Message:
Does anyone know what's going wrong? Thanks in advance.

selenium can't locate the element

selenium can't locate the element.
The error info:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"loadAllUpcomingPast"}
my code is:
url_base = 'http://www.christies.com/lotfinder/searchresults.aspx&searchtype=p&action=paging&searchFrom=header&lid=1&entry=&pg=all'
driver = webdriver.Chrome()
driver.get(url_base)
time.sleep(2)
driver.switch_to.frame("signupFrame")
driver.find_element_by_id("close_signup").click()
time.sleep(2)
driver.find_element_by_id("loadAllUpcomingPast").click()
the screenshot
Seems that the loadAllUpcomingPast gets loaded later on. You could try something like this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
url_base = 'http://www.christies.com/lotfinder/searchresults.aspx?&searchtype=p&action=paging&searchFrom=header&lid=1&entry=lama&pg=all'
driver = webdriver.Chrome()
driver.get(url_base)
wait = WebDriverWait(driver, 5)
try:
element = wait.until(EC.element_to_be_clickable((By.ID, 'loadAllUpcomingPast')))
print(f'Element found: {element}')
except TimeoutException:
print('could not find loadAllUpcomingPast')
In my console I see:
Element found: <selenium.webdriver.remote.webelement.WebElement (session="c9f1b83164f34001aa0a98581618d45b", element="0.6182317130585246-1")>
This will tell Selenium to wait until that element is clickable. You can adjust the wait time if needed within the WebDriverWait 2nd argument.

Trying to automate facebook login and writing a post on friends timeline

Here is the code attached:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.facebook.com/")
driver.find_element_by_name("email").send_keys("***EMAIL***")
driver.find_element_by_id("pass").send_keys("***PW***")
wait = WebDriverWait(driver, 15)
driver.find_element_by_xpath("//*[#id='loginbutton']").click()
wait = WebDriverWait(driver, 10)
ele = driver.find_element_by_name("q")
ele.send_keys("**FRIEND NAME**")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//*[#id='js_e']/form/button/i")))
element.click()
wait = WebDriverWait(driver, 5)
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//*[#id='js_9t']/img")))
element.click()
driver.find_element_by_xpath("//*[#id='js_3q9']/div[1]/div[1]/div[2]/div/div/div/div/div[2]/div/div/div/div").send_keys("Its Automated")
The code is not working and gets stuck after logging in or after searching my friend's name and sometimes not even writing my friend's name. It's different every time not able to figure out what to do. The element locators also change all of sudden. Below are the errors I'm getting:
1) selenium.common.exceptions.InvalidElementStateException: Message: invalid element state
2) raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
3) Message: stale element reference: element is not attached to the page document
The error is telling you exactly what the problem is. The element you are trying to access via xpath is not being found on the document/page. Consequently, Selenium waits for a bit before timing out.

Categories