Unable to click sign in/out button using python selenium - python-3.x

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()

Related

I'm writing a code where I can enter Instagram and get the follower list

Hello ı'm a beginner coder
I want to login to instagram using selenium and get my follower list but my code But the code I wrote does not give any error or output.
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class İnstagram:
def __init__(self,name,password):
self.browser = webdriver.Firefox()
self.username = username
self.password = password
def signIn(self):
self.browser.get("https://www.instagram.com")
time.sleep(1)
name = self.browser.find_element(By.XPATH,"//*[#id='loginForm']/div[1]/div[1]/div/label/input").send_keys(self.username)
psw = self.browser.find_element(By.XPATH,"//*[#id='loginForm']/div[1]/div[2]/div/label/input").send_keys(self.password)
self.browser.find_element(By.XPATH,"//*[#id='loginForm']/div[1]/div[3]").click()
time.sleep(7)
def getFollowers(self):
self.browser.get("https://www.instagram.com/emirhaninmalikanesi/followers/")
followers = self.browser.find_elements(By.CSS_SELECTOR,"._ab8w._ab94._ab99._ab9h._ab9m._ab9o._abcm")
for user in followers:
name = user.find_element(By.CSS_SELECTOR,".x1i10hfl.xjbqb8w.x6umtig.x1b1mbwd.xaqea5y.xav7gou.x9f619.x1ypdohk.xt0psk2.xe8uvvx.xdj266r.x11i5rnm.xat24cr.x1mh8g0r.xexx8yu.x4uap5.x18d9i69.xkhd6sd.x16tdsg8.x1hl2dhg.xggy1nq.x1a2a7pz.notranslate._a6hd").get_attribute("href")
print(name)
instagram = İnstagram(username,password)
instagram.signIn()
instagram.getFollowers()
Your code has two flaws
Before login you have to close the cookies dialog, otherwise the code raises ElementClickInterceptedException when trying to click on "Log in".
No use of selenium built-in timeout methods (driver.implicitly_wait or WebDriverWait) to wait for an element to be found. This causes followers to be an empty list.
Corrected code
driver.implicitly_wait(9) # driver waits up to 9 seconds for an element to be found
driver.find_element(By.XPATH,"//div[#role='dialog']/div/button[contains(.,'cookie')]").click()
driver.find_element(By.XPATH,"//*[#id='loginForm']/div[1]/div[1]/div/label/input").send_keys(username)
driver.find_element(By.XPATH,"//*[#id='loginForm']/div[1]/div[2]/div/label/input").send_keys(password)
driver.find_element(By.XPATH,"//*[#id='loginForm']/div[1]/div[3]").click()
driver.get("https://www.instagram.com/emirhaninmalikanesi/followers/")
followers = driver.find_elements(By.CSS_SELECTOR,"._ab8w._ab94._ab99._ab9h._ab9m._ab9o._abcm")
for user in followers:
name = user.find_element(By.CSS_SELECTOR,".x1i10hfl.xjbqb8w.x6umtig.x1b1mbwd.xaqea5y.xav7gou.x9f619.x1ypdohk.xt0psk2.xe8uvvx.xdj266r.x11i5rnm.xat24cr.x1mh8g0r.xexx8yu.x4uap5.x18d9i69.xkhd6sd.x16tdsg8.x1hl2dhg.xggy1nq.x1a2a7pz.notranslate._a6hd").get_attribute("href")
print(name)
As a final consideration, notice that you can avoid the login process by loading in selenium a user profile where you are already logged in. For more info look here https://stackoverflow.com/a/75434260/8157304

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()

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]

Scrolling the following and followers list with selenium(Python)

Hey I am trying to make a simple Insta bot using selenium.I reached up to my following list by automating but now I don't know how I can scroll down the list of my following/followers. I want to grab the account from my following and followers list and compare it and make a list of the accounts who havenot followed me back.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
from secrets import password,username
class Instabot:
def __init__(self,username,password):
self.driver = webdriver.Chrome(executable_path="C:\\Users\\user\\PycharmProjects\\chromedriver_win32\\chromedriver.exe")
self.driver.get("https://www.instagram.com")
sleep(5)
self.driver.find_element_by_xpath('//*[#id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input').send_keys(username) #searching the username box and giving it username
self.driver.find_element_by_xpath('//*[#id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input').send_keys(password)#searching the password box and giving it password
self.driver.find_element_by_xpath('//*[#id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[4]/button').click() #clicking login button
wait=WebDriverWait(self.driver,10)
notNowButton = wait.until(
lambda d: d.find_element_by_xpath('//button[text()="Not Now"]'))#it will click on first notnow button
notNowButton.click()
next_not_now=wait.until(lambda notnow:notnow.find_element_by_xpath('//button[text()="Not Now"]'))
next_not_now.click() #it will click on second not now button
def get_unfollowers(self):
wait = WebDriverWait(self.driver, 10)
clickprofile= wait.until(lambda a: a.find_element_by_xpath('//*[#id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[5]/a'))
clickprofile.click()
following_list = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href,'/following')]")))
following_list.click()#IT clicks the following and gives window of following listy
print("CLicked following list")
my_bot=Instabot(username,password)
my_bot.get_unfollowers()
I saw about execute_script() But I dont know what to put under those brackets.
Well I figure it out. This code works for me now!
fBody = self.driver.find_element_by_css_selector("div[class='isgrP']")
scrolling_times=(numoffollowers/4)
scroll=0
scroll_count = scrolling_times+5 # You can use your own logic to scroll down till the bottom
while scroll < scroll_count:
self.driver.execute_script(
'arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;',
fBody)
sleep(2)
scroll += 1
You can use something like that :
self.driver.execute_script("window.scrollTo(0, Y)")
with Y : how much you want to scroll in pixel.
It's up to you to see how far down you have to go down.

Reusing previously opened selenium window

I have been testing various automation tasks using Selenium Python library. I faced a very serious issue in that process. That is the delay caused due to poor internet connection.
The starting steps are same for all tasks, namely
- open browser
- go to website
- login
- open a specific link after login
These tasks take a long time, so I was hoping to find a method by which I could reuse an already open session and continue testing on that window instead of opening new browser window every time I run the script.
Here's what I have tested so far:
Example Original File:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(r'C:\testDir\chromedriver_win32\chromedriver.exe', chrome_options=chrome_options)
url = driver.command_executor._url
session_id = driver.session_id
print("URL:",url,"SESSION ID:",session_id)
driver.get('http://www.facebook.com')
username = "---"
password = "---"
driver.find_element_by_name('email').send_keys(username)
driver.find_element_by_name('pass').send_keys(password)
driver.find_element_by_name('login').click()
# time.sleep(2)
driver.maximize_window()
time.sleep(2)
driver.find_elements_by_name('q').send_keys('Ayush Mandowara')
# element = wait.until(EC.presence_of_element_located((By.xpath, '//input[#placeholder="Search"]'))
driver.find_element_by_xpath('//input[#placeholder="Search"]').send_keys('Ayush Mandowara' + Keys.RETURN)
time.sleep(4)
driver.find_element_by_xpath('//div[contains(text(), "Ayush Mandowara")]').click()
time.sleep(3)
driver.find_element_by_class_name('coverBorder').click()
time.sleep(2)
Connecting File:
from connectingToPrev import url, session_id
driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.session_id = session_id
driver.get("http://www.google.com")
This connecting file is following what all has already happened in the previous window, I was expecting it either connect to previous window or to open the last link with correct credentials
Answer in Python is appreciated!

Resources