This website prevent me from signing in, what should I do - python-3.x

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

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

Selenium Python - How to load existed profile on chrome?

So, I want to use my existed profile on chrome to make easily to login and fetch some data from the website. So I tried this on my current codes but it doesn't load the profile for some reason,
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
class ClientDriver:
def __init__(self):
self.options = Options()
self.options.add_argument("--lang=en_US")
self.options.add_argument("--disable-gpu")
self.options.add_argument("--no-sandbox")
self.options.add_argument("--disable-dev-shm-usage")
self.options.add_argument(
r"user-data-dir=C:\Users\User\AppData\Local\Google\Chrome\User Data\Profile 1"
)
self.options.add_argument("--profile-directory=Profile 1")
def check(self):
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()), options=self.options
)
driver.get("chrome://version/")
time.sleep(100)
x = ClientDriver()
x.check()
As can you see on my current codes, it will redirect to chrome://version/ to check the Profile Path and it's not C:\Users\User\AppData\Local\Google\Chrome\User Data\Profile 1 but it was C:\Users\Users\AppData\Local\Google\Chrome\User Data\Profile 1\Profile 1. Can someone help me out?
You're checking chrome://version/ from your selenium test, of course you see a wrong path!
You should instead open a normal chrome window, insert chrome://version/ in the search bar, press enter and check from there your profile path.
You will see it will be something like C:\Users\User\AppData\Local\Google\Chrome\User Data\Profile 1
And so in the code you'll have to write:
self.options.add_argument(
r"user-data-dir=C:\Users\User\AppData\Local\Google\Chrome\User Data"
)
self.options.add_argument("--profile-directory=Profile 1")

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

How to make sure the last message sent in WhatsApp python bot

I use the code I wrote to send a message on WhatsApp to several contacts, but before all the messages are sent, Chrome is closed and some messages are not sent. How can I be sure that the message I sent was sent?
(This is not a problem in the program) The problem is the low speed of the Internet and you have to wait a while for the message to be sent
from bs4 import BeautifulSoup
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import os
class WhatsApp:
def __init__(self) -> None:
user = os.getlogin()
options = webdriver.ChromeOptions()
# options.headless = True
options.add_argument('--profile-directory=Default')
options.add_argument(
f'--user-data-dir=C:\\Users\\{user}\\AppData\\Local\\Google\\Chrome\\User Data')
PATH = "chromedriver_win32\chromedriver.exe"
self.driver = webdriver.Chrome(
executable_path=PATH, chrome_options=options)
self.driver.set_window_position(0, 0)
self.driver.set_window_size(0, 0)
self.driver.get("https://web.whatsapp.com")
WebDriverWait(self.driver, 5000).until(
EC.presence_of_element_located(
(By.XPATH, '//*[#id="side"]/header/div[2]/div/span/div[2]/div'))
)
def send_msg(self, phone: str, msg: str):
search_btn = self.driver.find_element_by_xpath(
'//*[#id="side"]/header/div[2]/div/span/div[2]/div')
search_btn.send_keys(Keys.ENTER)
input_search = self.driver.find_element_by_xpath(
'/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/div/div[2]')
input_search.send_keys(phone, Keys.ENTER)
try:
sleep(1)
self.driver.find_element_by_xpath(
"/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]").send_keys(
msg, Keys.ENTER)
except Exception as e:
pass
# showMessageBox.contacte_not_found(showMessageBox)
def check_is_sended(self, phone: str, msg: str):
pass
#some code i need to check message sended or not
def END(self):
sleep(3)
self.driver.close()
app = WhatsApp()
phone = "+98xxxxxxxx87"
message = "test"
app.send_msg(phone , message)
app.END()
so I do not want to use sleep for long time im just want to find best way to make time short for runing program any id?
when selenium clicks "send msg button", a new div will be created in html that indicates that you sent a msg. so, make selenium wait until it appears.
then make selenium also wait for the verification icon that indicates that the msg has been sent successfully to the Whatsapp server.
Q: How do I get the new message ?
A: you can wait until the chat-room html-div contains the msg you sent.
from selenium.webdriver.support import expected_conditions as EC
locator = (By.ID, 'someid') #modify this according to your case
your_new_msg = 'blabla bla' #modify this according to your case
wait.until(EC.text_to_be_present_in_element(locator, your_new_msg))
but this short solution may lead to bug because the msg may be existing within another old msg in the chat-room div. so, selenium will think that it is the intended msg then exit the wait by mistake.
so, the solution for this may be hard for you if you are a new developer.
you need to make a channel between Python and Javascript to pass the data between them
then you create an Event Listener in JS to watch the chat-room changes (eg: you can override the (chat-room-div).appendChild method. then you get the new-msg html element in your JS method before it is shown on html)
you then can send a msg from JS to python to tell him that the new-msg div has appeared and the verification icon also appeared
then python will exit its waiting state. then continue executing the next code
_____
this is just the idea. the implementation of it is up to you.

Find the Twitter text box element with python selenium

I made my own Twitter complaint bot that tweets at my ISP if the network drops.
Code works perfect, until it has to find the Twitter textbox to type the tweet.
Main error is:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
I have tried:
Adding time delays
Using Firefox Driver instead of Google
Adding page refreshes before the tweet_at_provider() looks for the textbox
Clicking the "Tweet" button to bring up the textbox to then try type in it
Using find.element_by_id but twitter changes id every pageload
When I comment out the first function call to test, it will find and type 6/10 times.
But when both functions are called the tweet_at_provider() always fails at grabbing the textbox and I get the StaleElement error.
import selenium, time, pyautogui
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException, NoSuchElementException, StaleElementReferenceException
PROMISED_DOWN = 200
PROMISED_UP = 10
CHROME_DRIVER_PATH = "C:\Development\chromedriver.exe"
GECKODRIVER_PATH = "C:\\Users\\meeha\\Desktop\\geckodriver\\geckodriver.exe"
TWITTER_USERNAME = "my_username"
TWITTER_PASSWORD = "my_password"
class InternetSpeedTwitterBot():
def __init__(self, driver_path):
self.driver = webdriver.Chrome(executable_path=driver_path)
self.down = 0
self.up = 0
def get_internet_speed(self):
self.driver.get("https://www.speedtest.net/")
self.driver.maximize_window()
time.sleep(2)
go = self.driver.find_element_by_xpath("//*[#id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[1]/a/span[4]")
go.click()
time.sleep(40)
self.down = self.driver.find_element_by_xpath("//*[#id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[2]/div/div[2]/span")
self.up = self.driver.find_element_by_xpath("//*[#id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[3]/div/div[2]/span")
print(f"Download Speed: {self.down.text} Mbps")
print(f"Upload Speed: {self.up.text} Mbps")
time.sleep(3)
def tweet_at_provider(self):
self.driver.get("https://twitter.com/login")
self.driver.maximize_window()
time.sleep(3)
username = self.driver.find_element_by_name("session[username_or_email]")
password = self.driver.find_element_by_name("session[password]")
username.send_keys(TWITTER_USERNAME)
password.send_keys(TWITTER_PASSWORD)
password.submit()
time.sleep(5)
tweet_compose = self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/header/div/div/div/div[1]/div[3]/a/div/span/div/div/span/span')
tweet_compose.click()
time.sleep(2)
textbox = self.driver.find_element_by_xpath('//*[#id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
textbox.send_keys(f"Hey #Ask_Spectrum, why is my internet speed {self.down.text} down / {self.up.text} up when I pay for {PROMISED_DOWN} down / {PROMISED_UP} up???")
bot = InternetSpeedTwitterBot(CHROME_DRIVER_PATH)
bot.get_internet_speed()
bot.tweet_at_provider()
I had the same error there and figured out that HTML tag was instantly changing as soon as I was typing something on the twitter text-box.
tackle this problem using XPATH of span tag that was showing up after typing space from my side. break tag is the initial tag when there is not any text prompted by you, only after you type anything turns into and that's when you have to copy XPATH and use it for your application

Resources