Upload file with Selenium Webdriver Python - python

I have tried the method from this page:
Upload file with Selenium in Python
Code:
file_button = browser.find_element_by_id('fileUploadProxy')
file_button.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
But I get the following error:
Traceback (most recent call last):
File "test.py", line 110, in <module>
upload_students_results('Surname, Name')
File "test.py", line 91, in upload_students_results
file_button.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 349, in send_keys
'value': keys_to_typing(value)})
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
(Session info: chrome=58.0.3029.96)
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.4 x86_64)

The problem is - you are sending keys to the div element which is not "interactable", does not accept the keys - hence the "cannot focus element" error.
The idea behind the solution you've linked is to send keys to the input element with type="file" that is responsible for the file upload. Find this element in your HTML and send keys to it.
Note that this element could be invisible. In this case, you should first make it visible for the send_keys() to work.
Update:
Okay, now we at least know which element is our desired one:
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
Since you have troubles locating this element, either try waiting for it:
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
file_upload = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "fileToUpload2"))
)
file_upload.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
Or/and, check if this element is inside an iframe - if it is, you would need to switch into the context of the iframe and only then perform the element search.

I had the same problem when I inserted the file path as a string. This is functional:file_input.send_keys(os.path.abspath("path/to/the/file.xyz"))

Related

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

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:

Copy text from a table cell (td) with Selenium/Python

Hi I'm very new to Selenium and would really appreciate help.
I need to learn how to copy text, particularly from a table from a specific website: https://aca.tampagov.net/citizenaccess/Default.aspx#
(Go to url --> Search --> Building Permits --> click search button).
You can finds a table at the bottom of the page.
The first row in column "Record Type" has text "Commercial Utility Application". If I want to copy the text what I thought I should do is get the xpath which is
//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType"]
So I would have something like
driver.find_element_by_xpath('//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
But this gives me and error. What am I doing wrong here? And how would I be able to copy texts from such tables?
EDIT
So here's what I get:
Traceback (most recent call last):
File "<ipython-input-81-4e51d9229198>", line 1, in <module>
driver.find_element_by_xpath('//* [#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 387, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 957, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]"}
(Session info: chrome=67.0.3396.99) (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
Your table is in iframe, so you have to switch to it before you can interact with this table. This code snippet will help you to do it:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//*[#id='ACAFrame']")))
# do your stuff
text = driver.find_element_by_xpath('//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
print(text)
driver.switch_to.default_content() # switch back to default content
PS: since in the page there is only one table, you can simplify your xPath from:
//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]
to:
//tbody/tr[3]/td[4]
To retrieve text Commercial Utility Application, you should try -
driver.findElement(By.id("ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType")).getText();

Selenium finding hidden element

I'm having difficulty accessing a dropdown menu in www.meridiancu.ca. It's the one under "Select Banking Type" on the right side of the homepage. Once I run my code.
from selenium import webdriver
from selenium.webdriver.common import action_chains, keys
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.meridiancu.ca")
bank_type = driver.find_element_by_id('SelectAccount')
bank_type.click()
I get the following output, and I'm not sure how I can find this "hidden" element.
DevTools listening on ws://127.0.0.1:12015/devtools/browser/6f5fba77-4c41-49b9-93a3-64a8363cd35b
Traceback (most recent call last):
File "C:\Users\Imad\Documents\Programming\Python\dropdown select.py", line 14, in <module>
bank_type.click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=62.0.3202.94)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86_64)
Any suggestions or solutions would be greatly appreciated.
The following code for working with select tag:
from selenium.webdriver.support import ui
ui.Select(driver.find_element_by_css_selector(".sign-in-panel.sign-in-banner #SelectAccount")).select_by_visible_text("Small Business Banking")
Result: The "Small Business Banking" option should be selected from the dropdown.
Hope it helps you!
The issue you're encountering is that your selector is finding 2 elements that match. The first element that matches is hidden. The hidden element is displayed when the page is viewed by a mobile browser.
I did some investigating, and found that the following CSS selector will find the element you are looking for.
bank_type = find_element_by_css_selector('.show-for-large select#SelectAccount.banking-target')
You can also do the iteration over all options in the drop down, using the following code snippet.
el = driver.find_element_by_id('SelectAccount')
for option in el.find_elements_by_tag_name('option'):
if option.text == 'Personal Banking':
option.click() # select() in earlier versions of webdriver
break

Not able to click list item in a dropdown menu in selenium python

I want to click the excel button under export drop down button.But even I am able to access the menu.When I am trying to click on the excel option under it,a message is being displayed that unable to locate element.
Here is the exact error message:
Traceback (most recent call last):
File "C:/Users/shishir sinha/PycharmProjects/australia/australia.py", line 33, in
driver.find_element_by_xpath(".//[#id='ui-menu-0-1']").click()
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 309, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 787, in find_element
'value': value})['value']
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute
self.error_handler.check_response(response)
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, 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='ui-menu-0-1']"}
(Session info: chrome=58.0.3029.110)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.14393 x86_64)
Here is the link to website:
https://stats.oecd.org/Index.aspx?DataSetCode=STAN08BIS
Here is the code:
_author_ = 'shishir'
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.Chrome("C:\\Users\\shishir sinha\\AppData\\Local\\Programs\\Python\\Python36\\selenium\\webdriver\\chromedriver_win32\\chromedriver.exe")
driver.get("https://stats.oecd.org/Index.aspx?DataSetCode=STAN08BIS")
driver.find_element_by_xpath(".//*[#id='PDim_COU']").click()
driver.find_element_by_xpath(".//*[#id='PDim_COU']/option[1]").click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH, ".//*[#id='PDim_VAR']")))
driver.find_element_by_xpath(".//*[#id='PDim_VAR']").click()
action = webdriver.ActionChains(driver)
driver.find_element_by_xpath(".//*[#id='PDim_VAR']/option[3]").click()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, ".//*[#id='menubar-export']/a/span[1]/span[2]")))
driver.find_element_by_xpath(".//*[#id='menubar-export']/a/span[1]/span[2]").click()
driver.find_element_by_xpath(".//*[#id='menubar-export']/a/span[1]/span[2]").click()
action.move_to_element(driver.find_element_by_xpath(".//*[#id='menubar-export']/a/span[1]/span[2]"))
driver.find_element_by_xpath(".//*[#id='ui-menu-0-1']").click()
Most likely the actual <select> element is not visible on the page and the select you see on the page is a pseudo-select element made for example of <div> or <li> elements.
This is now becoming more common, given that there are limited options to style native selects. As a result many frameworks 'overlay' them with better looking components and delegate the logic to the hidden underlying select via javascript.
The solution will be to find the actual visible elements and click them instead of the <select> or use driver.execute_script(...) to drive the widgets via javascript.

Categories

Resources