Enter data into input field not working with selenium - python

I'm making basic script for sing-in into Instagram.
I faced with this error
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get('https://www.instagram.com/accounts/login/')
username = browser.find_element_by_name("username").send_keys('login')

Try to add
time.sleep(5)
before
username = browser.find_element_by_name("username").send_keys('login')
Might be page not fully loaded

The error reads 'Window not found. The browser window may have been closed'. It happened in line browser.find_element_by_tag_name("body"). The Instagram login page has two such elements, and you need only one of them. You should make the query more specific, for instance by making it return only the first element with the tag.

Related

Python Selenium problems with passing login information to user/password form

So, the last 3 hours or so I have tried to get Selenium to work without success. I managed to make it work with requests and Beautifulsoup, but apparently site uses javascript to load data after login so I cannot scrape the data I want after successful login.
Below is the script I am trying to work with.
``
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("/home/user/Desktop/chromedriver")
username = "FakeUsername"
password = "PasswordFake"
driver.get("https://www.helen.fi/kirjautuminen")
time.sleep(10)
# find username/email field and send the username itself to the input field
# find password input field and insert password as well
driver.find_element(By.XPATH,'//input[#id="username"]').send_Keys(username)
# click login button
``
(Yes, I know its missing password and submit actions, but I can't get it to write anything into username or password input boxes. Also same script seems to be working fine with github's login page, so I really can't understand what I am doing wrong here).
After running the script, chrome opens, site loads fine, but for some reason I get error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id="username"]"}
I have tried with element ID and Name with similar errors, except it said something about "unable to locate element: css selector ..."
If anyone has some advice to give a newbie, it would be awesome. This is starting to give me headache.
I except the script to write username into username input box, but nothing happens.
this occurs because there is an iframe in the page code. It is necessary to switch to the iframe and then search for the element, follow the code with the correction.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path= "chromedriver.exe")
username = "FakeUsername"
password = "PasswordFake"
driver.get("https://www.helen.fi/kirjautuminen")
# find username/email field and send the username itself to the input field
# find password input field and insert password as well
iframe = driver.find_element(By.XPATH, '//iframe[#class="login-iframe"]')
driver.switch_to.frame(iframe)
driver.find_element(By.XPATH,'//input[#id="username"]').send_keys(username)
# click login button

Upload picture in selenium without input[type="file=] element

I'm trying to automate the process to upload the pictures on remove.bg but I'm unable to find the input field for the file in inspect elements. I know that by using input and send_keys we can automate this but what should I do in this case when the input field is not visible. This is what I've tried till now.
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver = webdriver.Firefox()
test = driver.get("https://www.remove.bg/upload")
input = driver.find_element_by_xpath('//input[#type="file"]')
print (input)
#send_keys below.
it looks like it is a production system. Do you have permission to automate this site?

Python: Cannot find object with Selenium

Can somebody please help me identify the Login/email, password, and LOG IN elements on this site?
https://lo12poznan.mobidziennik.pl/
I tried using multiple methods but my code couldn't detect them.
I just need the element identification...
I was able to simulate an incorrect login using this script below - if you provide the correct login and password, I think this would work
Script as been written using selenium==4.0.0b4 [ install it using pip install selenium==4.0.0b4]
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc)
driver.maximize_window()
driver.get("https://lo12poznan.mobidziennik.pl/dziennik/")
time.sleep(5)
driver.find_element(By.ID,'login').send_keys('warsaw#polska.pl')
driver.find_element(By.ID,'haslo').send_keys('MyCryptic#PAASWORD098')
driver.find_element(By.CSS_SELECTOR,"input[type='submit']").click()
driver.quit()
CSS:
Login/e-mail [id="login"]
password [id="haslo"]
submit .btn.btn-block
With XPath style the locators are:
login input field
//input[#id='login']
password input field
//input[#id='haslo']
submit button
//input[#type='submit']
You can use other unique combinations there as well.
And you can use other approaches i.e. by css selector.
Don't forget adding some wait / delay before accessing the elements to let the page loaded before accessing elements there.
you can use find_element_by_id
id for user name is login
id for password is haslo
and for submit button, use find_by_css_selector :-
input[class*='btn-lg']
Remember, xpath usages is not prefer in automation if we happen to find ids. or css.

Logging into website using selenium with python

I am trying to use selenium to log into this website:
but it says the password and login are not visible. I looked around and saw that some people said to wait, but waiting does not seem to help. Here is my code:
# importing libraries
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re, time, csv
driver = webdriver.Firefox()
driver.get("https://platform.openquake.org/account/login/")
driver.switch_to
driver.maximize_window
time.sleep(10)
username = driver.find_element_by_xpath("//input[#name='username']")
username.send_keys("hi there")
Error message is :
ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Your XPATH actually matches two elements. The non-plural driver methods (find_element_by_XXX) return the first element they find a match for, which in this case is not the one you want.
A good debugging tool for situations like this is to use the plural forms (find_elements_by_XXX) and then see how many elements matched.
In this case, you should do what Tanu suggested and use a more restrictive XPATH:
username = driver.find_element_by_xpath("//div[#class='controls']/input[#id='id_username']")
password = driver.find_element_by_xpath("//div[#class='controls']/input[#id='id_password']")
Modify your xpath:
username = driver.find_element_by_xpath("//div[#class='controls']/input[#id='id_username']")

Selenium Python - Getting the current URL of web browser?

I have this so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\Users\Fan\Desktop\chromedriver.exe')
url = driver.current_url
print url
It keeps saying that line 4 "driver" is an invalid syntax. How would I fix this?
Also is there a way I can get all the current tabs open, and not just a single one?
EDIT: the above code works now; But I have another problem!
The code now opens a new tab, and for some reason the URL bar has "data;" in it, and it outputs data; as the print.
But I want it to take the existing URL from existing web browser already opened, how do I solve this?
In Python you do not specify the type of variable as is required in Java which is the reason for the error. The same error will also happen because your last line starts with String.
Calling webdriver.Chrome() returns a driver object so the line webdriver driver = new webdriver() is actually not needed.
The new keyword is not used in Python to create a new object.
Try this:
from selenium import webdriver
driver = webdriver.Chrome()
url = driver.getCurrentUrl()
In order to extract the url of the current page from the web driver you have to call the current_url attribute:
from selenium import webdriver
import time
driver = webdriver.Chrome()
#Opens a known doi url
driver.get("https://doi.org/10.1002/rsa.1006")
#Gives the browser a few seconds to process the redirect
time.sleep(3)
#Retrieves the url after the redirect
#In this case https://onlinelibrary.wiley.com/doi/abs/10.1002/rsa.1006
url = driver.current_url

Categories