linkedin boot selenium python - python

hi i try to write a bot that automates once a day. Logged into my LinkedIn chat with a person who sends me posts and every message i wiil have post and bot need to to respond my email and write send my you cv something similar to this.
def start():
driver.get("https://www.linkedin.com")
time.sleep(3)
username = driver.find_element(By.ID, 'session_key')
username.send_keys(MyUsername)
time.sleep(0.7)
password = driver.find_element(By.ID, "session_password")
password.send_keys(MyPassword)
time.sleep(0.3)
button_sign = driver.find_element(By.XPATH, '//*[#type="submit"]')
button_sign.click()
time.sleep(3)
# scroll_down_random()
button_messaging = driver.find_element(By.XPATH, '//*[#id="global-nav"]/div/nav/ul/li[4]/a')
button_messaging.click()
time.sleep(2)
search_chat = driver.find_element(By.XPATH, '//*[#id="search-conversations"]')
search_chat.send_keys(name_of_chat)
search_chat.send_keys(Keys.ENTER)
time.sleep(2)
submit_application = driver.find_element(By.ID,'//*[#id="ember65"]')
submit_application.click()
but I am stuck on two things one After I search I can't select the first chat and I Run in a loop between messages in posts.
after
submit_application = driver.find_element(By.ID,'//*[#id="ember65"]')
submit_application.click()
need to click the first person after I search. and how I can sty always connected if I upload it to the cloud without having to connect every time

I think the ID you are searching for is incomplete, I get an ID like
"overlay-conversation-card-ember133"
This might be different for different devices so I believe you should use XPATH here rather than the ID.
Hope this helps. 👍🏻
EDIT:
It seems selenium .click() does not always register, use .submit() although this doesn't work for all browsers incase of that use .send_keys(Keys.ENTER)

Related

Finding Valid Telegram Channel using selenium python

Hello good morning/evening! i want to ask, is there a way to differentiate this two type of way telling that those channels does not exist? I'm creating a tool to find existing channel on telegram. Here are the examples.
When i try to run my code, the channels that does not exist still counts as an existing channel.
Here is the code that use, to differentiate this 2 type of way telegram telling that the channel does not exist. I used the help of selenium and chrome driver to find the existing telegram channel.
for ss in cli1:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(f"https://t.me/{ss}")
try:
if WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/div/div[1]/div[2]/a/div'))):
print(f'URL https://t.me/{ss} is not available')
Not_Exist.append(ss)
elif WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/div[2]/a'))):
print(f'URL https://t.me/{ss} is not available')
Not_Exist.append(ss)
except:
print(f'URL https://t.me/{ss} is valid')
exists.append(ss)
driver.quit()
sleep(5)
Is there a way for a telegram channels that does not exist still say no available, instead of making the site available but it doesn't have any channel at all.Here are the link examples. Non existing site 1 https://t.me/minecraft Existing Site https://t.me/minecraftapkdownload Non Existing site 2https://telegram.org/
Thanks for the help!
In your code, in the try block, make the below changes
try:
if WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSSSELECTOR, 'tgme_action_button_new shine'))):
print(f'URL https://t.me/{ss} is valid')
exists.append(ss)
except:
print(f'URL https://t.me/{ss} is not available')
Not_Exist.append(ss)
I would recommend you to use path made up of classnames/ids etc.
Paths like/html/body/div[2]/div[2]/div/div/div[1]/div[2]/a/div are prone to throw errors even with minimal changes to the GUI
In the above code, I have assumed that the View in Telegram button is shown if a channel is valid and for all other pages no such button is shown.
The class for the button is 'tgme_action_button_new shine'
The code checks if this button exists, if yes then it adds the page to the valid list, else to the invalid list

Open whatsapp on a headless browser

Use case:
Sending out automated WhatsApp messages using pythonanywhere. Step by step logic below:
non-coders write on a gsheet the phone numbers to which we should
send out the messages
the gsheet data is read (using gspread in pythonanywhere)
open WhatsApp URL to send out the messages in bulk
I have a code using selenium running on my machine that opens the web whatsapp url, finds the needed elements on the website, and sends the messages to the gsheets phone numbers - find below a snippet from that part of the code that I am using on my machine:
global driver
driver.get('https://web.whatsapp.com/')
waiter.until(EC.title_is("WhatsApp"))
waitCounter = 0
while 1:
try:
waiter.until(EC.presence_of_element_located((By.XPATH, "//canvas[#aria-label='Scan me!']")))
waitCounter+=1
if waitCounter%1000 == 0:
print("Waiting for user to log in...", 'WARNING')
except:
print("Logged in to WhatsApp")
break
for entry in data:
driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(str(entry['PhoneNumber']))
time.sleep(2)
driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(Keys.ENTER)
time.sleep(2)
driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message']))
time.sleep(2)
driver.find_element_by_class_name('_4sWnG').click()
time.sleep(2)
Doubt:
To make step nr 3. working on python anywhere I would have to use a headless browser. However, to initiate whatsapp web we always need to do the QR code scan, so I am not being able to do it that way. Find below the current (useless) part of my code with the headless selenium code - (NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='side']/div[1]/div/label/div/div[2]"}). I am quite stuck here. Any tip or idea to overcome this is more than welcome and happy to discuss any possible solutions using other libraries that you guys might find appropriate.
Thanks in advance.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=chrome_options)
def send_whatsapp_message():
global driver
driver.get('https://web.whatsapp.com/')
print("Done updating, check the spreadsheet now")
#redirect('https://web.whatsapp.com/', code=302)
for entry in data:
driver.find_element_by_xpath("//*[#id='side']/div[1]/div/label/div/div[2]").send_keys(str(entry['PhoneNumber']))
time.sleep(2)
driver.find_element_by_xpath("//*[#id='side']/div[1]/div/label/div/div[2]").send_keys(Keys.ENTER)
time.sleep(2)
driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message']))
time.sleep(2)
driver.find_element_by_class_name('_4sWnG').click()
time.sleep(2)
print("Successfully send message to {0}, name: {1}".format(str(entry['PhoneNumber']), str(entry['Name'])), 'INFO')

Stuck over here making Instagram bot

This is my code:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# loading the webpage
browser = webdriver.Chrome()
browser.get("https://instagram.com")
time.sleep(1)
# finding essential requirements
user_name = browser.find_element_by_name("username")
password = browser.find_element_by_name("password")
login_button = browser.find_element_by_xpath("//button [#type = 'submit']")
# filling out the user name box
user_name.click()
user_name.clear()
user_name.send_keys("username")
# filling out the password box
password.click()
password.clear()
password.send_keys("password")
# clicking on the login button
login_button.click()
time.sleep(3)
# information save permission denial
not_now_button = browser.find_element_by_xpath("//button [#class = 'sqdOP yWX7d y3zKF ']")
not_now_button.click()
time.sleep(3)
# notification permission denial
not_now_button_2 = browser.find_element_by_xpath("//button [#class = 'aOOlW HoLwm ']")
not_now_button_2.click()
time.sleep(3)
# finding search box and searching + going to the page
search_box = browser.find_element_by_xpath('//input [#placeholder="Search"]')
search_box.send_keys("sb else's page")
time.sleep(3)
search_box.send_keys(Keys.RETURN)
search_box.send_keys(Keys.RETURN)
time.sleep(3)
# opening ((followers)) list
followers = browser.find_element_by_xpath('//a [#class="-nal3 "]')
followers.click()
time.sleep(10)
# following each follower
follower = browser.find_elements_by_xpath('//button [#class="sqdOP L3NKy y3zKF "]')
browser.close()
In this code, I normally simulate what a normal person does to follow another person.
I want to follow each follower of a page. I have thought all day long; But couldn't find any algorithms.
Got some good ideas, but just realized I don't know how I can scroll down to the end of the list to get the entire list. Can you help? (If you don't get me, try running the code and then extract the list of followers.)
# following each follower
get list of followers
for each follower - click 'follow' if it's possible
if button text haven't changed, it means that you reached the limit of follows, or maybe banned
Also, be sure to limit your actions, instagram had limit of follows (30 per hour, it was before)
And you can get the followers directly through instagram API.
And don't forget to unfollow them, because unfollowing also has limits. And the limit of current follows is 7500( it was before, not sure how about now)
First you need to get a list of the users that follows someone, then you just execute the same code in a loop. You can either scrape the users separate, or within selenium. Then run the code needed to follow a given person, in Ex. a for loop. Step 6: profit

sending message on youtube live chat with selenium using python

I was trying to make a script using python and selenium, to spam a massage on youtube live stream for a giveaway, i was able to do most of the task successfully until on line (33), where i was trying to locate the youtube live chat box by(.find_element_by_...?) it was showing all kind of error like,AttributeError: 'WebDriver' object has no attribute or failed to locate element_by_...? 'find_element_by_text,xpath,id.class, etc
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
email = ''
password = ''
gmail_link = 'http:\\www.gmail.com'
driver = webdriver.Firefox('D:\Projects\python projects')
driver.get(gmail_link)
time.sleep(4)
#email send_keys
driver.find_element_by_id('identifierId').send_keys(email)
#Email_next button xpath.click
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[2] /div/div/div[2]/div/div[2]/div/div[1]/div/content/span').click()
time.sleep(4)
#password xpath
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[1]/div/form/content/section/div/content/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input').send_keys(password)
time.sleep(2)
#password button xpath.click
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[2] /div/div/div[2]/div/div[2]/div/div[1]/div/content/span').click()
time.sleep(5)
#A random youtube live link
driver.get('https://www.youtube.com/watch?v=VTqTnbe6b1g')
time.sleep(5)
# youtube live chat box
driver.find_element_by_text('Say somthing...').send_keys('#PUBGMOBILE')
time.sleep(3)
# youtube live chat box button
driver.find_element_by_id('//*[#id="button"]').click()
Basically that div is associated with 'input' event so you have to dispatch the 'input' event once you enter the value in the div.
Here is the code which I was able to execute successfully form console (FYI this is JS you can use the same logic in your test and language).
ele = document.querySelector('div#input')
ele.textContent = 'test this sample data'
ele.dispatchEvent(new Event('input',{'bubles':true, 'cancelable':true}))
Line 1 is equals findelemnt by css
Line 2 enter the input
Line 3 fireevent 'input' on the element
WebElement chatEle = driver.find_element(By.CSS_SELECTOR('div#input')
chatEle.sendKeys "sample data"
driver.fireEvent(chatEle,"input")
should do the magic, couldn't test the code above as I don't have eclipse java environment on my machine. Test it and let us know.
Refer to Selenium FireEvent document for more information on the fireevent implementation.
Instead of looking for the element by its text you could use its xpath like you did before. I just looked at the chatbox for a YT livestream and the xpath seems to be called //*[#id="input"]
Edit:
Also, there could be something tricky where you need to click the element first before you can enter text.
don't use the [#id="input"] it was also given for another element too. You will not get the element. Use class name {yt-live-chat-text-input-field-renderer style-scope} this is unique you will get the element by this class name.
Your sending text to a wrong element
driver.find_element_by_text('Say somthing...').send_keys('#PUBGMOBILE')
This is lable not a textbox.

Selenium: How to deal with the situation where being treated as robots

I am using Selenium to simulate login with Chrome browser. The login page has Slider button which needs to be dragged to complete the login process.
The weird thing is that if I open the web page using driver.get(page). Then sliding verification code will always fail even I manually enter the username, pwd and drag the slider.
I tried to add user-agent but no help.
Is there any other way? I understand that we can add user-data to skip this part but I just want to figure it out first.
The slider verification code will display if you try to log in more than once. It seems that the web page just detect you are using the script to log in.
The error will be like:" ops, failed to verify. Please refresh again". Then I use driver.find_element_by_xpath("//*[#id='nocaptcha']/div/span/a").click() to refresh the slider.
Thanks in advance.
driver = webdriver.Chrome()
driver.get("https://login.taobao.com/member/login.jhtml?spm=a21bo.2017.754894437.1.5af911d95pqPfs&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F")
action = ActionChains(driver)
login()
def login():
driver.find_element_by_class_name("login-switch").click()
driver.find_element_by_id('TPL_username_1').clear()
driver.find_element_by_id('TPL_username_1').send_keys('xxx')
driver.find_element_by_id('TPL_password_1').clear()
driver.find_element_by_id('TPL_password_1').send_keys("xxx")
time.sleep(1.5)
# driver.find_element_by_id('J_SubmitStatic').click()
Slider()
def Slider():
#Sliding verification code
while True:
try:
slip=driver.find_element_by_xpath("//*[#id='nc_1_n1z']")
action.click_and_hold(slip).perform()
action.move_by_offset(150,0)
time.sleep(0.8)
action.move_by_offset(148,0)
action.release().perform()
time.sleep(2)
text = driver.find_element_by_xpath("//*[#id='nc_1__scale_text']/span")
if text.text.startswith("请在下方"):
print("Successful")
break
if text.text.startswith("请点击"):
print("Successful")
break
if text.text.startswith("请按住"):
print("Failed. Try again")
continue
except Exception:
##Error occurs, click the "Refresh" button to refresh the sliding verification code again
driver.find_element_by_xpath("//*[#id='nocaptcha']/div/span/a").click()
driver.find_element_by_id('J_SubmitStatic').click()

Categories