Getting error while uploading the file using selenium - python

This is the error when i click on OKI am trying to automate process with selenium in python so one of the step is to upload file below is the input element before selenium tried to upload the file:
<input ct="FU" lsdata="{1:'Content',7:'ONSUBMIT',8:true}" lsevents="{Change:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" id="WD039B" name="WD039B" type="file" tabindex="0" ti="0" class="lsFileupload__input urBorderBox urUpld" size="0" title="Content">
When selenium upload the file and click on "OK" i am getting error like file has not been chosen but i can see the file name against "Choose File" below is the same input element when selenium tried to click on "OK" after uploading:
<input ct="FU" lsdata="{0:'ERROR',1:'Content',7:'ONSUBMIT',8:true}" lsevents="{Change:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" id="WD030D" name="WD030D" type="file" tabindex="0" ti="0" class="lsFileupload__input urBorderBox urUpldInv" size="0" title="Content">
When i manually tried to upload file after uploading same element looks like this:
<input ct="FU" lsdata="{1:'Content',7:'ONSUBMIT',8:true}" lsevents="{Change:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" id="WD0302" name="WD0302" type="file" tabindex="0" ti="0" class="lsFileupload__input urBorderBox urUpld" size="0" title="Content" style="background-image: none;">
I am using following selenium code to upload the file:
upload_element=WebDriverWait(driver,v[3]/table/tbody/tr/td/div/div[1]/table/tbody/tr[2]/td/div/div/div/div/table/tbody/tr[1]/td[2]/form/input")))
time.sleep(5)
#driver.execute_script("arguments[0].removeAttribute('style')", upload_element)
time.sleep(5)
#To upload the file
upload_element.send_keys("C:\\Users\\e0481737\\.PyCharmCE2018.2\\config\\scratches\\Help File.pdf")
driver.execute_script("arguments[0].style.display = 'background-image: none;'; return arguments[0];", upload_element)
# until this point I am successful but when I click on "OK" it says that file is not chosen
time.sleep(5)
# to click on "OK" button
element = WebDriverWait(driver, wait_time).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/table/tbody/tr/td/div/div[1]/div/div[4]/div/table/tbody/tr/td[3]/table/tbody/tr/td[1]/div")))
time.sleep(5)
element.click()

Related

Selenium Webdriver is unable to find any element in a webpage

Trying to write a webscraper but I need to get into a website that requires an account. I'm going through 2 factor authentication to get in as that felt more secure than by username and password but I can't get Selenium to find the input for the 2fa code.
I tried every type of locater available and none of those work. The website does not appear to have a frame so I don't think that's the issue. I'm using an implicit wait as well so I believe the webpage has enough time to load.
I'm not really sure what I'm doing wrong at this point. I just keep getting an Unable to locate element error
Here the html for the item
<input class="sc-jzJRlG hxEkIM sc-VigVT gupvKM" data-testid="security-code" id="security-code" value="" autocomplete="off" autocorrect="off" spellcheck="false" maxlength="255" type="password" data-reactid=".0.1.0.2.1.1.0.0.1.0">
and my code
driver.implicitly_wait(10)
codeInput = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input')
enterButton = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[2]/div/button')
codeInput.send_keys('123456')
action.click(enterButton)
action.perform()
and the Stacktrace
Message=Message: Unable to locate element: /html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input
Stacktrace:
RemoteError#chrome://remote/content/shared/RemoteError.jsm:12:1
WebDriverError#chrome://remote/content/shared/webdriver/Errors.jsm:192:5
NoSuchElementError#chrome://remote/content/shared/webdriver/Errors.jsm:404:5
element.find/</<#chrome://remote/content/marionette/element.js:291:16
Source=C:\Users\user\source\repos\WebScraper\WebScraper\webscraper.py
StackTrace:
File "C:\Users\user\source\repos\WebScraper\WebScraper\webscraper.py", line 27, in <module> (Current frame)
codeInput = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input')

Clicking the hidden input Selenium in Python

I am trying to automate the login procedure with Selenium in Firefox with Python.
That's how a login button looks like in HTML:
<td>
<input name="cmd" value="lg" type="hidden">
<input src="ok.png" style="border-style: none;" type="image">
</td>
I have tried a following method:
loginButton = driver.find_elements_by_xpath("//input[#name='cmd' and #value='lg']")[0]
loginButton.click()
It returns the following exception with an empty message.
"selenium.common.exceptions.ElementNotInteractableException: Message: "
This method returns
"Message: Element is not visible"
loginButton = driver.find_element_by_name("cmd")
loginButton.send_keys(Keys.RETURN)
Could you please explain what I am missing?
If you want to click on input next to hidden, try
loginButton = driver.find_element_by_xpath("//input[#src='ok.png']")
# loginButton = driver.find_element_by_xpath("//input[#name='cmd' and #value='lg']/following-sibling::input")
loginButton.click()

Clicking on an image in selenium

I'm having an issue clicking on an image using the Chromedriver with Selenium for the following HTML:
<div class="modal_buttons">
<input type="image" name="GoButton" id="GoButton" tabindex=14 title="Continue" alt="Continue" onfocus="SetLastFocus(this.id)" src="https://t4.ftcdn.net/jpg/00/81/83/05/240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg"
I tried using the following code:
element = driver.find_element_by_css_selector("//div[img/#src='https://t4.ftcdn.net/jpg/00/81/83/05/240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg']").click()
Selenium is failing everytime, and giving the same list of errors that it can't locate the button. Any ideas how to fix?
Thanks
Try:
driver.find_element_by_xpath("//input[#id='GoButton'][#name='GoButton']").click()
or
driver.find_element_by_xpath("//input[#id='GoButton'][#title='Continue']").click()
or
driver.find_element_by_xpath("//input[#name='GoButton'][#title='Continue']").click()
or
driver.find_element_by_xpath("//input[contains(#src,'240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg')]")

Canot click button with Python with Selenium

I am trying to click a button that brings up a dialogue box to select a file. inspecting the element it looks like its an input rather than a button. Either way I cannot click it with:
element = browser.find_element_by_id("fileupload")
element.click()
and
browser.find_element_by_id("fileupload").send_keys("\n")
Neither of which seem to work.
Here is what I see when I inspect that element on the page:
<span class="btn btn-success fileinput-button">
<span class="glyphicon glyphicon-upload"></span>
Select and Upload...
<input id="fileupload" name="upfile" accept=".xml" type="file">
</span>
Any assistance help or pointers would be appreciated!
Clicking on a file input usually triggers a file upload dialog. Since you cannot control it with selenium, you need to avoid the dialog being opened by sending keys to the input instead:
browser.find_element_by_id("fileupload").send_keys("path_to_the_file")
See also:
How to deal with file uploading in test automation using selenium or webdriver
How to upload file using Selenium WebDriver in Java
How to upload file ( picture ) with selenium, python

Selenium - How to debug timeout for iframe > input field?

I'm stuck writing a Selenium WebDriver script for Instagram web login. I think I switched to the appropriate iframe but WebDriver keeps timing out when it should locate the user input field.
Relevant source from Instagram site:
https://instagram.com/accounts/login/
<iframe class="hiFrame" data-reactid=".0.0.0.1.0.1.0.0.$frame" src="https://instagram.com/accounts/login/ajax/?targetOrigin=https%3A%2F%2Finstagram.com" scrolling="no" seamless="">
<!DOCTYPE html>
<html class="hl-en not-logged-in " lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<body class="LoginFormChrome ">
<div class="LoginFormPage" data-reactid=".0">
<form data-reactid=".0.0">
<p class="lfField" data-reactid=".0.0.0">
<label class="lfFieldLabel" data-reactid=".0.0.0.0">
<input class="lfFieldInput" type="text" data-reactid=".0.0.0.1" value="" autocorrect="false" autocapitalize="false" maxlength="30" name="username">
</p>
Source from Selenium script:
login_url = 'https://instagram.com/accounts/login/'
profile_url = '<path_firefix_profile>'
user = '<user_name>'
#login
my_profile = FirefoxProfile(profile_url)
self.driver = webdriver.Firefox(my_profile)
self.driver.get(login_url)
self.driver.implicitly_wait(10)
my_iframe = self.driver.find_element_by_css_selector("iframe.hiFrame")
#my_iframe = self.driver.find_element_by_css_selector("iframe:nth-of-type(1)")
#my_iframe = self.driver.find_element_by_tag_name("iframe")
self.driver.switch_to_frame(my_iframe)
try:
element = WebDriverWait(self.driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
user_input = self.driver.find_element_by_css_selector("input[name='username']")
user_input.send_keys(user)
finally:
print('user name input appeared')
Results:
This error results from WebDriver:
File "instagram_firefox.py", line 51, in setUp
element = WebDriverWait(self.driver, 45).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 71, in until
raise TimeoutException(message)
I tried to verify that the css selector for the input field was correct. On the page, https://instagram.com/accounts/login/, FireFox FireFinder does not recognize the css selector that I used. But if I open another tab with the source of the iframe, https://instagram.com/accounts/login/ajax/?targetOrigin=https%3A%2F%2Finstagram.com, then Firefinder recognizes the css selector that I used. Does this mean I need to manually get the url of the iframe source or should that be done automatically when WebDriver switches to the iframe?
We should wait first for div spinner element to disappear, then we can retrieve that iframe you need:
user = "user"
self.driver.get("https://instagram.com/accounts/login/")
#Wait for spinner to disappear
WebDriverWait(self.driver, 10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.liSpinnerLayer")))
#Get iframe and switch to it
my_iframe = self.driver.find_element_by_css_selector("iframe.hiFrame")
self.driver.switch_to_frame(my_iframe)
element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
element.send_keys(user)

Categories