how to download linkedin (save as pdf option) using python - python

Image what i want to download.
Image is of LinkedIn profile page of my friend
i want to click on that save-as-pdf option for many users.
can that be downloaded using python code? for different users?
or can it be downloaded using any other language?

Yes you can automate this with python that works for every profile, so you dont have to worry about ids changing
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
login = "Email address goes here"
password = "Type your password here"
#start browser session
chromedriver = "/home/romtein/chromedriver" #change this to your selenium driver
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#open linkedin in automated browser
driver.get("https://www.linkedin.com/")
time.sleep(1)
#logs you into Linkedin
driver.find_element_by_id("login-email").send_keys(str(login))
password = driver.find_element_by_id("login-password").send_keys(str(password))
driver.find_element_by_id("login-submit").click()
print("successfully logged in")
#navigates to your connections
time.sleep(1)
driver.get("https://www.linkedin.com/mynetwork/invite-connect/connections/")
time.sleep(1)
#opens a new tab of your top contact
ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').perform()
contact_page_open = driver.find_element_by_class_name("mn-connection-card__name").click()
time.sleep(2)
#switch to new tab
driver.switch_to_window(driver.window_handles[1])
time.sleep(1)
# ActionChains(driver).key_up(Keys.CONTROL).perform()
#click the "more" button
driver.find_element_by_class_name("pv-s-profile-actions__overflow").click()
time.sleep(1)
#saves profile to pdf
driver.find_element_by_class_name("pv-s-profile-actions pv-s-profile-actions--save-to-pdf").click()
time.sleep(1)
Let me know if you have any questions

Related

I can't get pass recaptcha with selenium infinitely stuck

I am working on a auto form submitting project I tried selenium but I could fill the forms but when I click submit button a recaptcha being summoned, when I tried to bypass it with 2captcha but didn't work and after that I tried to do it manually but when I do it correctly it gets reseted can you help me with my code?
import time
import os
import requests
import random
import string
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver as wd
import sys
def random_char(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))
mail = (random_char(7)+"#gmail.com")
driver = wd.Firefox(executable_path='C:\geckodriver.exe')
target_url = 'https://www.yemeksepeti.com/login/new?step=registration'
driver.get(target_url)
driver.maximize_window()
time.sleep(2)
print(mail)
time.sleep(2)
email = str(mail)
time.sleep(2)
firstName = 'Mert'
time.sleep(2)
lastName = 'Demir'
time.sleep(2)
birthDate = '01-04-2001'
time.sleep(2)
password = 'AbcAbc123.*_123'
time.sleep(2)
emailPath = driver.find_element("xpath", '//*[#id="email"]')
time.sleep(2)
emailPath.send_keys(mail)
time.sleep(2)
firstNamePath = driver.find_element("xpath", '//*[#id="first_name"]')
time.sleep(2)
firstNamePath.send_keys(firstName)
time.sleep(2)
lastNamePath = driver.find_element("xpath", '//*[#id="last_name"]')
time.sleep(2)
lastNamePath.send_keys(lastName)
time.sleep(2)
birthDatePath = driver.find_element("xpath", '//*[#id="birthdate"]')
time.sleep(2)
birthDatePath.send_keys(birthDate)
time.sleep(2)
passwordPath = driver.find_element("xpath", '//*[#id="password"]')
time.sleep(2)
passwordPath.send_keys(password)
time.sleep(2)
Submit = driver.find_element("xpath", '//*[#id="login-page-react-root"]/main/div/form/div[8]/button')
Submit.click()
time.sleep(2)
The main reason for having a re-captcha is to stop bots (ie: selenium automation scripts) from interacting with the web page/form.
If you can bypass that this is a big red light for the application security. And is not recommended in standard QA practices.
What you need to do is to have your dev environment configured to be test friendly. There are multiple ways this can be achieved. From my personal experience :
Remove the captcha field for dev/test environment forms.
Have the developers setup a static captcha which always has one known captcha phrase
Test the feature with the captcha manually
I'm sure there are a lot of other ways to overcome this situation, but forcing the script to enter the real captcha is not the way to go

Python using selenium webdriver to fill a form, when submitting the form, chrome is saying no internet connection is available

I am using Selenium webdriver to fill out a form, the fields are good, but when I do a .submit using the login button, or do a .submit after the password field, the chrome browser will say "Unable to connect to internet. Please check your internet connection."
the internet is definitely up since I am able to open other websites on another browser (including a new session of chrome).
This is my code:
import time
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions();
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation']);
options = Options()
# options.add_argument('start-maximumized')
# options.add_argument('disable-infobars')
chrome_options.add_argument('no-sandbox')
PATH = "c:\scripts\chromedriver.exe"
# driver = webdriver.Chrome(executable_path=PATH)
driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path='c:\scripts\chromedriver.exe')
driver.get('https://stupidwebsite.org')
time.sleep(1)
input_username = driver.find_element_by_id('username')
time.sleep(1)
input_username.send_keys("myself#email.com")
input_password = driver.find_element_by_id('password')
input_password.send_keys("mypassword#123")
input_password.submit()
# esubmit=driver.find_element_by_xpath
('/html/body/div/div[1]/div[2]/div/div/form/div[2]/div/button')
# esubmit = driver.find_element_by_name('MuiButton-label')
# esubmit.submit()
time.sleep(2)
# driver.quit()
I changed it a bit, and tried out google, and it works:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\scripts\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.google.com/")
#identify search box
m = driver.find_element_by_name("q")
#enter search text
m.send_keys("Tutorialspoint")
time.sleep(0.2)
#perform Google search with Keys.ENTER
m.send_keys(Keys.ENTER)
Please help me out. Thank you.

Trying to sign into google using a selenium python script

I am using selenium to open and sign into google accounts as my first step. I have successfully opened and filled the email response although upon submitting I receive the error of
"This browser or app may not be secure. Learn more Try using a
different browser. If you’re already using a supported browser, you
can refresh your screen and try again to sign in." From google.
Is there any way to get around this? Here is my code below.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://accounts.google.com/")
print(driver.title)
search = driver.find_element_by_name("identifier")
search.send_keys("email goes here")
search.send_keys(Keys.RETURN)
Was having the same issue and i found this thread in GitHub.
The solution that worked for me was to use this driver: undetected_chromedriver instead of the normal ChromeDriver.
import undetected_chromedriver.v2 as uc
chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("user_agent=DN")
self.browser = uc.Chrome(options=chrome_options)
self.browser.delete_all_cookies()
# example of loggin in to youtube without getting that issue
self.browser.get('http://youtube.com')
login_button_init = self.browser.find_element_by_xpath("//a[#aria-label='Sign in']")
login_button_init.click()
# locate the login button
login_button = self.browser.find_element_by_xpath("//paper-button[#aria-label='Sign in']")
login_button.click()
# get email and set to email input box
email = self.browser.find_element_by_id("identifierId")
myemail = os.environ.get('YOUTUBE_EMAIL')
email.send_keys(myemail)
# click next button
email_next_button = self.browser.find_element_by_id("identifierNext")
email_next_button.click()
# get password and set to password input box
password = self.browser.find_element_by_name("password")
mypassword = os.environ.get('YOUTUBE_PASSWORD')
password.send_keys(mypassword)
sleep(2)
# click next button to log in
pass_next_button = self.browser.find_element_by_id("passwordNext")
pass_next_button.click()

How can I take multiple (a lot) of inputs from a user and repeat code with each input the user gives? (Python)

First, I apologize if I make/made any mistakes, or sound silly. I have only just started learning python about a week ago and I'm trying to build a web scraping program with the little knowledge I currently have. There may be some useless time.sleep functions and things, but I have changed and tested many things to get my script to work, so please ignore those things, unless there are critical errors in my code, or stuff I can improve.
So I have written this script using selenium with intent to take video links from the app "TikTok," and run them through a TikTok video downloader website to download these videos. I have successfully written a script that can be used with one link at a time. For example, the user inputs a link and the script opens the website, pastes the link, submits, and then downloads the video from the link.
I am wondering how I can turn this script into one where the user may input many many links, (up to 100) and repeat the downloading process until there are no more links. I would also like for my program to display the current link in use on the terminal, and say "Successfully Downloaded" every time a video is downloaded. My current script code is below. Any help is greatly appreciated. Thank you.
# imports
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
# set chrome options
options = Options()
options.headless = False
# path of chrome webdriver
PATH= 'C:\Program Files (x86)\chromedriver.exe'
# print text and ask for user link
print('TikTok Auto Download Links BOT')
print('-' * 30)
link_url = input('Enter link to be downloaded: ')
# set browser driver and open window with select size
browser = webdriver.Chrome(PATH, options = options)
browser.set_window_size(1920, 1080)
browser.maximize_window()
browser.get('https://snaptik.app/en-us')
time.sleep(1)
# find url box and paste user's input then submit
url_field = browser.find_element_by_id('url')
url_field.send_keys(link_url)
url_field.submit()
time.sleep(3)
# click download button
download_button = browser.find_element_by_xpath('//*[#id="div_download"]/section/div/div/div/article/div[2]/div/a[1]')
time.sleep(3)
download_button.click()
# print successful for UI
time.sleep(3)
print('Download successful! ')
# wait before closing
time.sleep(3)
# close browser
browser.quit()
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
# set chrome options
options = Options()
options.headless = False
# path of chrome webdriver
PATH= 'C:/Users/COUNT DEXTER/Downloads/chromedriver_win32/chromedriver.exe'
# print text and ask for user link
print('TikTok Auto Download Links BOT')
print('-' * 30)
set_download_limit = 100 #download limit
i=1 #counter
while i<=set_download_limit: #while condition
link_url = input('Enter link to be downloaded: ')
# set browser driver and open window with select size
browser = webdriver.Chrome(PATH, options = options)
browser.set_window_size(1920, 1080)
browser.maximize_window()
browser.get('https://snaptik.app/en-us')
time.sleep(1)
# find url box and paste user's input then submit
url_field = browser.find_element_by_id('url')
url_field.send_keys(link_url)
url_field.submit()
time.sleep(3)
# click download button
download_button = browser.find_element_by_xpath('//*[#id="div_download"]/section/div/div/div/article/div[2]/div/a[1]')
time.sleep(3)
download_button.click()
# print successful for UI
time.sleep(3)
print('Download successful! ')
i+=1 #increment counter, then return to loop
# wait before closing
time.sleep(3)
# close browser
browser.quit()```

Chromedriver to pickup existing Chrome browser session in Python

I want to open several weblinks under a website in 1 browser (several tabs). The website requires login and password.
When login and password keyed in. it turns to a verification page, asks for the verification code sent to me by email.
I checked the email and key in verification code on the verification page. Login is successful.
The existing browser is in front of me.
However the codes are not picking it up, and open another tab as wanted. Seems a certain connection is lost.
How can I continue? (or as an alternative, how can Python to reuse the existing Chrome browser?)
The codes usually works well but comes to this case (login, enter verification code), it doesn't.
import os, time
from selenium.webdriver import ChromeOptions, Chrome
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "C:\\Python27\\Scripts\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
opts = ChromeOptions() # leave browser open after code
opts.add_experimental_option("detach", True) # leave browser open after code
opts.add_argument('disable-infobars')
driver = webdriver.Chrome(chromedriver, chrome_options=opts) # leave browser open after code
driver.maximize_window()
verificationErrors = []
accept_next_alert = True
time.sleep(5)
base_url = "https://awebsite.com/"
driver.get(base_url)
window_0 = driver.window_handles[0]
driver.switch_to_window(window_0)
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("username")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("password")
driver.find_element_by_id("Submit").click()
time.sleep(60)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
window_1 = driver.window_handles[1]
driver.switch_to_window(window_1)
time.sleep(3)
driver.get('https://anotherwebsite.com')
time.sleep(3)
sys.exit()
You can try below to perform some actions on two different pages/tabs:
# Handle base page
base_url = "https://awebsite.com/"
driver.get(base_url)
window_0 = driver.current_window_handle
...
# Handle new page
driver.execute_script('window.open("https://anotherwebsite.com");')
window_1 = [window for window in driver.window_handles if window != window_0][0]
driver.switch_to_window(window_1)
# driver.close() # To close new tab
...
# Switch back to base page
driver.switch_to_window(window_0)

Categories