Website login loading endlessly when I use find_element() - python-3.x

I'm having a problem with Selenium, when fetching an input from a login from a page. For some reason, whenever I store the element in a variable, the site in question, when trying to log in, keeps loading infinitely. However, if I remove that part of the code, and enter the login credentials manually, the login is done normally. I will explain below:
This is how the page input I try to access looks like:
<input id="txtUsername" name="txtUsername" class="random-name" placeholder="User" type="text" required="required" aria-required="true" autocomplete="off" autocorrect="off" autocapitalize="off">
<input id="txtPassword" name="txtUsername" class="random-name" placeholder="User" type="text" required="required" aria-required="true" autocomplete="off" autocorrect="off" autocapitalize="off">
And this is my python code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import chrome
from time import sleep
options = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=options)
site = "www.google.com"
browser.get(site)
def make_login(browser):
sleep(1)
login = browser.find_element(By.ID, "txtUsername")
login.click()
login_text = "User"
for x in login_text:
sleep(0.15)
login.send_keys(x)
senha = navegador.find_element(By.ID, "txtPassword")
senha.click()
senha_text = "Password"
for x in senha_text:
sleep(0.15)
senha.send_keys(x)
if __name__ == "__main__":
make_login(browser)
When I run it, it clicks on each input, enters the password, as it should. However, when I click log in, the website keeps loading endlessly.
If i remove this part:
login = browser.find_element(By.ID, "txtUsername")
login.click()
senha = navegador.find_element(By.ID, "txtPassword")
senha.click()
And manually clicking on the inputs, he enters the site normally...
I appreciate anyone who can help me.

Have you tried to use only use send_keys()? So, your code will look like:
def make_login(browser):
sleep(1)
login = browser.find_element(By.ID, "txtUsername")
login_text = "User"
login.send_keys(login_text)
senha = navegador.find_element(By.ID, "txtPassword")
senha_text = "Password"
senha.send_keys(senha_text)
As shown in selenium documentation send_keys() method is enough to fill input fields.
But if you're trying to access sites like bet365 it's almost certainly the website prohibiting your code. Take a look at this post and its answers.

Related

Unable to click sign in/out button using python selenium

I'm trying to create a code where I can auto sign in and sign out on HR Portal i.e. "https://newage.greythr.com/". However, I'm unable to click the sign in/out button after logging in. Initial part of the code is working fine but the bottom part i.e. # Sign in/out is giving InvalidArgumentException error. I've tried 2 alternatives both mentioned below in the code but none of it is executing and giving the same error. Also, I tried to increase the wait time still it failed.
If anything is required from my end kindly let me know in comments section.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:/Users/Selenium Drivers/chromedriver.exe')
# Open the website
driver.get("https://newage.greythr.com/")
time.sleep(2)
# Defining username & password
username = "****"
password = "****"
time.sleep(2)
# Entering username
user = driver.find_element("id", "username")
user.click()
user.send_keys(username)
time.sleep(2)
# Entering password
psw = driver.find_element("id", "password")
psw.click()
psw.send_keys(password)
time.sleep(2)
# Click login button
driver.find_element("id", "password").submit()
# Sign in/out
time.sleep(10)
driver.find_element("XPATH", "//button[contains(.,'Sign In')]").click()
**Upper part is same as above**
# Sign in/out
sign = driver.find_element("XPATH", "/html/body/app/ng-component/div/div/div[2]/div/ghr-home/div[2]/div/gt-home-dashboard/div/div[2]/gt-component-loader/gt-attendance-info/div/div/div[3]/gt-button[1]")
time.sleep(20)
sign.click()
You should be using 'xpath' argument or import the By module and do it like
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, "//button[contains(.,'Sign In')]").click()

This website prevent me from signing in, what should I do

I am having error when I try to sendkeys(Keys.ENTER) if I am using python to login this blibli website. But when I try to login normally from chrome browser, I can login normally without this error.
I can input the username and password with selenium python, but then it just ignores the sendkeys(Keys.Enter) command. I have tried adding the login_button.click() command, but the selenium seems to ignore the click() command too. Then, when I tried to click the login button manually, I got an error: "Yah, ada eror nih. Coba lagi, ya." Meaning: "There is an error. Please try again, ok.".
It seems like the website can detect if I am using automated software, thus prevent me from logging into the website, eventhough it is my own account. I have attached the error screenshot. The error prevents me from clicking enter or clicking the blue login button ("Masuk").
Can anybody solve this? Thank you.
The process is complete, because I got this response:
Process finished with exit code 0
This is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from time import sleep
import os
CHROME_DRIVER_PATH = "C:\Development\chromedriver_win32\chromedriver.exe"
URL = "https://www.blibli.com/login"
EMAIL = os.environ['EMAIL']
BLIBLI_PASSWORD = os.environ["BLIBLI_PASSWORD"]
class Blibli:
def __init__(self):
self.driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
def login(self):
self.driver.get(URL)
sleep(3)
username = self.driver.find_element_by_class_name("login__username")
username.send_keys(EMAIL)
# actions = ActionChains(self.driver)
# actions.move_to_element(username).send_keys(EMAIL)
password = self.driver.find_element_by_css_selector('input[type="password"]')
password.send_keys(BLIBLI_PASSWORD, Keys.ENTER)
login_button = self.driver.find_element_by_class_name("blu-btn")
login_button.click()
def get_data(self):
pass
bot = Blibli()
bot.login()
bot.get_data()

Page keep loading infinitly in try except statement of function

During automating of review posting, I am stuck in a situation where a function having try-except statement. You may get my point clearly by reading below code.
This method writes reviews and return True or False
def write_review(browser,row):
try:
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'label[for="rating-5"]'))).click()
sleep(1)
browser.find_element_by_id("review.userNickname").clear()
browser.find_element_by_id("review.userNickname").send_keys(row['nickname'].replace(' ',''))
sleep(1)
browser.find_element_by_id("review.title").clear()
browser.find_element_by_id("review.title").send_keys(row['headline'])
sleep(1)
browser.find_element_by_id("review.reviewText").clear()
browser.find_element_by_id("review.reviewText").send_keys(row['review'])
sleep(1)
browser.find_element_by_css_selector('label[for="review.netPromoterScore.10"]').click()
sleep(1)
browser.find_element_by_css_selector('button[class*="js-preview"]').click()
sleep(3)
print('Submitting a review....')
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[class*="js-submit"]'))).click()
sleep(5)
return True
except Exception as ex::
print(str(ex))
return False
In case if any review is not posted successfully, I have implemented function call like this
browser.get(url)
success = write_review(browser,row)
if success==False:
while success!=True:
browser.get(url)
print('Again submitting...')
success = write_review(browser,row)
But the problem with above code is that in case of unsuccessful review posting, the page keep loading again and again without having entered any data in review body. Actually the error occurs at the line after print('Submitting a review....') statement which means click action before this line sometimes doesn't load javascript correctly. That's why I loaded that page again with the same statement of function call after while loop. Here are some error messages
Continue to review posting....
Submitting a review....
Submitting a review....
Submitting a review....
Submitting a review....
Message: element not visible
(Session info: chrome=89.0.4389.114)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.8.0-49-generic x86_64)
Again submitting...
Message: unknown error: Element <label for="rating-5" class="">...</label> is not clickable at point (468, 17). Other element would receive the click: <input id="search-autocomplete" type="text" name="query" autocomplete="off" autocorrect="off" placeholder="Search food, toys, prescriptions and more" role="combobox" aria-controls="search__suggestions" aria-haspopup="listbox" aria-label="Search food, toys, prescriptions and more" class="sfw-search__input">
(Session info: chrome=89.0.4389.114)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.8.0-49-generic x86_64)
Again submitting...
Message: unknown error: Element <label for="rating-5" class="">...</label> is not clickable at point (468, 18). Other element would receive the click: <input id="search-autocomplete" type="text" name="query" autocomplete="off" autocorrect="off" placeholder="Search food, toys, prescriptions and more" role="combobox" aria-controls="search__suggestions" aria-haspopup="listbox" aria-label="Search food, toys, prescriptions and more" class="sfw-search__input">
(Session info: chrome=89.0.4389.114)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.8.0-49-generic x86_64)
Place print/log inside 'except' block with exception info message and check why exceptions keep rising. Muting any exception especially global without logging error message is really bad idea.
Example:
try:
# Your code
except Exception as ex:
print(str(ex))
return False
If you want to prevent a infinite loop,you can try this
browser.get(url)
success = write_review(browser,row)
if success==False:
trytimes = 0
maxtrytimes = 10
while success!=True:
browser.get(url)
print('Again submitting...')
success = write_review(browser,row)
trytimes+=1
if trytimes>maxtrytimes:
print("write_review failed too many times")
break
If you want to click the button:
Import:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Add wait:
wait = WebDriverWait(driver, timeout=30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".a[class="js-submit"]")))
list_view = driver.find_element_by_css_selector('a[class="js-submit"]')
list_view.click()
You are using presence_of_element_located but it will not wait for the button completely.
Next, remove loop and remove sleeps.
Replace all sleeps with explicit wait like mentioned above, that will wait for the element that takes most time to load. Use EC.visibility_of_element_located to wait for an element to become visible.
You are ddosing the form because some locators are not completely loaded or some locators are incorrect. time.sleep(1) before each input is a very bad solution.
And finally, check if a[class="js-submit"] is unique.

Getting error message with using find_element_by_name from Selenium

I am using selenium to automate few tasks. My code till now just opens browser and gets username and password to fill them later but find_element_by_name is not working properly.
from selenium import webdriver
import time
driver = webdriver.Chrome(r'C:\Users\tarek\Desktop\namshi\chromedriver.exe')
driver.get("https://my.namshi.com/")
time.sleep(5)
try:
username = driver.find_element_by_name('email')
password = driver.find_element_by_name('password')
print(username)
except:
print('failed')
Why would you need to get the password or email? Do you mean you want to click on the fields and fill them with the two queries? If so, then you need to call the click method on your username and password because the find_element_by_name function returns a web element instead of finding it (which would be way more convenient, to be honest)
Basically:
def log_in(email, password) -> Webelement (a class):
username = driver.find_element_by_name('email')
username.click()
username.send_keys(email, Keys.RETURN)
password = driver.find_element_by_name('password')
password.click()
password.send_keys(password, Keys.RETURN)
try:
log_in()
except:
print('failed')
You have at least three elements with name email, I have tried this XPath:
//*[#name='email']
Your element is the second one I guess:
(//input[#name='email'])[2]

Selenium and Python 3: selecting a search box on otto.de

I am new to Python 3 as well as web scraping and I am a bit stuck right now.
I want to:
1. Select the search box on otto.de.
2. Insert the product number I want to search for.
3. Press enter or click the search button.
4. Download the following page.
The search field on otto.de has the following source code:
<form class="p_form js_searchForm focus" action="/suche" data-article-number-search="/p/search/" autocomplete="off" autocorrect="off" spellcheck="false" role="search">
<input placeholder="Suchbegriff / Artikelnr. eingeben" data-error="Bitte mind. ein Zeichen eingeben" class="p_form__input js_searchField sanSearchInput" type="text" autocomplete="off" autocorrect="off" maxlength="50" disabled>
<button class="sanSearchDelBtn p_symbolBtn100--4th" type="reset"><i>X</i></button>
<button class="js_submitButton sanSearchButton" type="submit" title="Suche" disabled ><span>ยป</span></button>
</form>
What I tried to do:
browser = webdriver.Firefox()
browser.get('http://www.otto.de')
elem = browser.find_element_by_xpath("//form[input/#class='p_form__input js_searchField sanSearchInput']")
elem.send_keys('538707' + Keys.RETURN)
with open("Productpage.txt", "w") as outfile:
outfile.write(browser.page_source)
browser.quit()
It gives me the following error message:
selenium.common.exceptions.InvalidElementStateException: Message: Unable to clear element that cannot be edited: <form class=
"p_form js_searchForm focus">
I tried many different commands but I just can't get on the page I need. Does anyone have an idea how to solve this problem?
Here is a working code:
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
browser = webdriver.Firefox()
browser.get("http://www.otto.de")
ui.WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".p_form__input.js_searchField.sanSearchInput"))).send_keys("538707")
ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".js_submitButton.sanSearchButton"))).click()
with open("Productpage.txt", "w", encoding="utf-8") as outfile:
outfile.write(browser.page_source)
time.sleep(5)
browser.quit()
Hope it helps you!

Resources