unable to locate element with selenium - python

I'm trying to locate the username input panel on this website:http://mail.qq.com
Here is the code I used:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get("http://mail.qq.com")
delay = 3
try:
WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('u')))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"
Here is the error message I got:
Traceback (most recent call last):
File "D:/Python27/3.py", line 10, in <module>
WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('u')))
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 285, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 787, in find_element
'value': value})['value']
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute
self.error_handler.check_response(response)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: [id="u"]
Does anyone know what problem there is?

Authentication form located inside an iframe. To be able to handle input field you should switch to that iframe first:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get("http://mail.qq.com")
delay = 3
browser.switch_to.frame("login_frame")
try:
WebDriverWait(browser, delay).until(EC.presence_of_element_located(('id', 'u')))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"

The exception is thrown from browser.find_element_by_id('u'), the driver tries to locate the element before entering the expected condition. Since it doesn't exists yet there is NoSuchElementException.
In addition, presence_of_element_located should receive locator as parameter, not WebElement
WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'u')))

Related

Selenium and Python - Bot for Instagram Login

I'm trying to create a bot to access instagram, but I don't understand why it's giving an error, here's the code:
from time import sleep
from selenium import webdriver
browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
login_link.click()
sleep(2)
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys("username")
password_input.send_keys("12345678")
login_button = browser.find_element_by_xpath("//button[#type='submit']")
login_button.click()
sleep(5)
browser.close()
and the error on cmd is this:
Traceback (most recent call last):
File "C:\Users\*******\Desktop\bot instagram da net.py", line 9, in <module>
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
File "C:\Users\*******\AppData\Local\Programs\Python\Python38-32\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\*******\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\*******\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\*******\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //a[text()='Log in']
I TRIED PUT A SLEEP AFTER BROWSER.GET, BUT HAD THE SAME ERROR
The following works for me:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.maximize_window()
driver.get('https://www.instagram.com/')
wait.until(ec.element_to_be_clickable((By.NAME, "username"))).send_keys("username")
el = wait.until(ec.element_to_be_clickable((By.NAME, "password")))
el.send_keys("password")
el.send_keys(Keys.ENTER)
Try having a sleep immediately after the browser.get. I think the page hasn't finished loading when you are trying to access the element.

Selenium not finding link element

I'm trying to click the "Login" button on this website, with Selenium : https://results.decisiondeskhq.com/2020/primary/colorado/president. I right clicked the element in inspect element, copied the xpath, and put it into the find_element_by_xpath function.
Here's my code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="/users/aliallam/Desktop/scraper test/chromedriver")
url = 'https://results.decisiondeskhq.com/2020/primary/colorado/president'
driver.get(url)
driver.find_element_by_xpath('//*[#id="content"]/div/div/div/div/button').click()
This is the error message I get:
Traceback (most recent call last):
File "/Users/aliallam/Desktop/scraper test/sandbox2.py", line 7, in <module>
driver.find_element_by_xpath('//*[#id="content"]/div/div/div/div/button').click()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/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":"//*[#id="content"]/div/div/div/div/button"}
(Session info: chrome=80.0.3987.149)
Thank you in advance!
You need to wait until the webpage load to select the button. You need to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
adding the delay and wait before select the element like this:
delay = 10 # seconds
WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.CLASS_NAME, 'signup-boxes')))
# you can select element that you want ini here
for more resource visit here
The element has an unique id, so instead of using xpath, you should use the id and you should apply explicit wait on the element so that the script waits until the element is present and as there is a div of element present above the element that you are trying to click, you need to use java script click in this case.
Your code should be like:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path="/users/aliallam/Desktop/scraper test/chromedriver")
url = 'https://results.decisiondeskhq.com/2020/primary/colorado/president'
driver.get(url)
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "login-text-btn")))
driver.execute_script("arguments[0].click();", element)

Element not interactable when trying to access a pop-up with Selenium

I am trying to scrape the following website: https://www.nemlig.com/ but it is not as easy as I was used to, as the page I am trying to scrape things off is not static. What I am trying to do using Selenium is click this:
So that the zipcode pop-up is visible. Then, insert a number and hit enter.
This is my take on it:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome(executable_path=r"C:\Users\user\lib\chromedriver_77.0.3865.40.exe")
browser.get('https://www.nemlig.com/')
elem = browser.find_element_by_xpath("//div[#class='timeslot-statusbutton']")
elem.clear()
elem = browser.find_element_by_xpath("//input[#class='prompt__input ng-pristine ng-valid ng-empty ng-touched']")
elem.send_keys("2300")
elem.send_keys(Keys.RETURN)
But everything after browser.get returns me this error:
Traceback (most recent call last):
File "", line 8, in
elem = browser.find_element_by_xpath("//div[#class='timeslot-statusbutton']").click()
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotInteractableException: element not interactable
(Session info: chrome=77.0.3865.90)
How can I do this properly?
You can try this code :
driver = webdriver.Chrome(executable_path = r'C:/Users/***/Downloads/BrowserDriver/chromedriver_win32/chromedriver.exe')
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("https://www.nemlig.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".timeslot-prompt.initial-animation-done")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='tel'][class^='pro']"))).send_keys('ABC')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.prompt__button"))).click()
imports will be :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

clicking checkbox using python selenium

I have a little task of scraping data from https://www.carecredit.com/doctor-locator/ .
I am unable to perform checkbox tick using my script.
I am doing
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.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
driver = webdriver.Chrome()
driver.get('https://www.carecredit.com/doctor-locator/')
driver.find_element_by_xpath("//select[#id='dl-
profession']/option[#value='9']").click()
driver.find_element_by_xpath("//*[#id='specialty-106']").click()
and getting error as
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
driver.find_element_by_xpath("//*[#id='specialty-106']").click()
File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 70, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 404, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 195, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 170, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: unknown error: Element <input type="checkbox" id="specialty-106" name="Specialty[]" value="106"> is not clickable at point (281, 554). Other element would receive the click: <label for="specialty-106"></label>
(Session info: chrome=58.0.3029.110)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 6.1.7600 x86_64)
Here is the Answer to your Question:
This code block will open the URL https://www.carecredit.com/doctor-locator, click on Profession Dropdown, select Weight Loss and finally select the checkbox Weight Loss Surgery.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
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(executable_path= r"C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get('https://www.carecredit.com/doctor-locator/')
select = Select(driver.find_element_by_id('dl-profession'))
select.select_by_value("9")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='specialty-106']//following::label[1]")))
driver.find_element_by_xpath("//input[#id='specialty-106']//following::label[1]").click()
Let me know if this Answers your Question.
The traceback contains a very clear explanation of what's wrong.
WebDriverException: Message: unknown error: Element is
not clickable at point (281, 554). Other element would receive the
click:
You might have to add a delay between your two clicks, to wait for the DOM to update so that the element is clickable.

Selenium Python 3.4.3 Loging into Gmail: Password section

So below is my code so far. It is supposed to go to the website and login with all the necessary details but i keep on getting huge chunks of errors! I dont understand where the error is hiding, anyone who sees it please help. Thanks!
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
browser = webdriver.Firefox()
from selenium.webdriver.common.keys import Keys
#browser.get('https://mail.yahoo.com')
browser.get('https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=en&service=mail#identifier')
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('email_address')
nextButton = browser.find_element_by_id('next')
nextButton.click()
passwordElem = browser.find_element_by_id('Passwd')
wait = WebDriverWait(browser, 10)
passwordElem = wait.until(EC.visibility_of_element_located((By.ID, "Passwd")))
passwordElem.clear()
passwordElem.send_keys('12345')
SignIn = browser.find_element_by_id('signIn')
SignIn.click()
#passwordElem.clear()
#passwordElem.send_keys('12345')
#SignIn = browser.find_elements_by_id('signIn')
#SignIn.click()
#passwordElem.submit()
This is the error i am receiving:
Traceback (most recent call last):
File "C:/Users/hp-450/Desktop/Automating_Gmail.py", line 15, in <module>
passwordElem = browser.find_element_by_id('Passwd')
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 266, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 744, in find_element
{'using': by, 'value': value})['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"Passwd"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/hp-450/AppData/Local/Temp/tmp3awlgy5b/extensions/fxdriver#googlecode.com/components/driver-component.js:10770)
at FirefoxDriver.prototype.findElement (file:///C:/Users/hp-450/AppData/Local/Temp/tmp3awlgy5b/extensions/fxdriver#googlecode.com/components/driver-component.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/hp-450/AppData/Local/Temp/tmp3awlgy5b/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/hp-450/AppData/Local/Temp/tmp3awlgy5b/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///C:/Users/hp-450/AppData/Local/Temp/tmp3awlgy5b/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
After you enter your email and submit the form, wait for the password field to be visible:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# wait for the password field to be visible
wait = WebDriverWait(browser, 10)
passwordElem = wait.until(EC.visibility_of_element_located((By.ID, "Passwd")))
passwordElem.clear()
passwordElem.send_keys('12345')
SignIn = browser.find_element_by_id('signIn')
SignIn.click()
Also note that you have to use find_element_by_id() instead of the find_elements_by_id() method to locate the "Sign In" button.

Categories