Automatically open and do some actions on web pages in python using different packages
1) Automatically open web url using Selenium-
get chrome driver from here-
https://chromedriver.storage.googleapis.com/index.html?path=80.0.3987.106
place the chrome driver in user/username/bin location
from selenium import webdriver
browser = webdriver.Chrome(executable_path = '/Users/abcarp/bin/chromedriver')
browser.get('https://in.linkedin.com/')
sleep(10)
browser.close()
2) Automatically login to Linkedin and do some action using selenium-
from selenium.webdriver.common.keys import Keys
# url of LinkedIn
url = "https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin"
# path to browser web driver
driver = webdriver.Chrome( '/Users/esisarp/bin/chromedriver')
driver.get(url)
# getting the user-name element
username = driver.find_element_by_id("username")
# Sending the keys for username
username.send_keys("uremail@gmail.com")
# Getting the password element
password = driver.find_element_by_id("password")
# Sending the keys for password
password.send_keys("urpassword")
# Getting the tag for submit button
driver.find_element_by_class_name("login__form_action_container").click()
# to click on network button
driver.find_element_by_id("mynetwork-tab-icon").click()
locate info about locating elements using selenium in python-
https://www.techbeamers.com/locate-elements-selenium-python/#locate-element-by-class-name
2) Automatically open web url using webbrowser package and move using pynput package
import webbrowser
webbrowser.open('http://machinelearningstories.blogspot.com/')
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (100, 200)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position-
mouse.move(5, -5)
sleep(10)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
3) Open and close browser using subprocess package
import subprocess
p = subprocess.Popen([r"/Volumes/Firefox/Firefox.app", "http://www.google.com"])
p.kill()
4) Closing the browser using os package-
import os
os.system("taskkill /im chrome.exe /f") #( windows)
os.system("pkill -f Chrome") # mac
5) Open the browser using mechanize package
import mechanize
br = mechanize.Browser()
br.open("http://machinelearningstories.blogspot.com/")
br.close()
6) How to randomly open multiple sites ( Impression( Page-View) bot)
! pip install random
! pip install webbrowser
import random
import webbrowser
import os
ls=['/2016/08/business-analytics-on-sales-data-of.html', '/recommendation-engine-market-basket.html', '/2016/08/optimizing-e-commerce-site-using.html'
,'/2016/08/8-simple-ways-to-increse-your-blog.html', '/2016/08/business-analytics-on-sales-data-of.html',
'/2017/12/gaussian-state-space-model-in-r-using.html', '/2017/09/assumptions-of-linear-regression.html',
'/2017/09/neural-network-forward-propagation-in.html', '/2018/06/lda-linear-discriminant-analysis-where.html',
'/2018/10/religious-demographics-of-india-in.html', '/2020/04/forecasting-total-deaths-from.html',
'/2019/11/automatic-ticket-resolution-for.html', '/2019/09/sentiment-analysis-using-nltk-and.html', '/2019/07/deep-learning-with-h2o-in-python.html',
'/2019/07/how-to-survive-in-data-science-and.html']
# change browser name below ( other than chrome)-
from time import sleep
url='https://machinelearningstories.blogspot.com'
for i in range(30):
item=random.choice(ls)
updated_url= url+item
print(updated_url)
webbrowser.get('chrome').open(updated_url)
sleep(5)
os.system("pkill -f Chrome")
get chrome driver from here-
https://chromedriver.storage.googleapis.com/index.html?path=80.0.3987.106
place the chrome driver in user/username/bin location
from selenium import webdriver
browser = webdriver.Chrome(executable_path = '/Users/abcarp/bin/chromedriver')
browser.get('https://in.linkedin.com/')
sleep(10)
browser.close()
2) Automatically login to Linkedin and do some action using selenium-
from selenium.webdriver.common.keys import Keys
# url of LinkedIn
url = "https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin"
# path to browser web driver
driver = webdriver.Chrome( '/Users/esisarp/bin/chromedriver')
driver.get(url)
# getting the user-name element
username = driver.find_element_by_id("username")
# Sending the keys for username
username.send_keys("uremail@gmail.com")
# Getting the password element
password = driver.find_element_by_id("password")
# Sending the keys for password
password.send_keys("urpassword")
# Getting the tag for submit button
driver.find_element_by_class_name("login__form_action_container").click()
# to click on network button
driver.find_element_by_id("mynetwork-tab-icon").click()
locate info about locating elements using selenium in python-
https://www.techbeamers.com/locate-elements-selenium-python/#locate-element-by-class-name
2) Automatically open web url using webbrowser package and move using pynput package
import webbrowser
webbrowser.open('http://machinelearningstories.blogspot.com/')
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (100, 200)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position-
mouse.move(5, -5)
sleep(10)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
3) Open and close browser using subprocess package
import subprocess
p = subprocess.Popen([r"/Volumes/Firefox/Firefox.app", "http://www.google.com"])
p.kill()
4) Closing the browser using os package-
import os
os.system("taskkill /im chrome.exe /f") #( windows)
os.system("pkill -f Chrome") # mac
5) Open the browser using mechanize package
import mechanize
br = mechanize.Browser()
br.open("http://machinelearningstories.blogspot.com/")
br.close()
6) How to randomly open multiple sites ( Impression( Page-View) bot)
! pip install random
! pip install webbrowser
import random
import webbrowser
import os
ls=['/2016/08/business-analytics-on-sales-data-of.html', '/recommendation-engine-market-basket.html', '/2016/08/optimizing-e-commerce-site-using.html'
,'/2016/08/8-simple-ways-to-increse-your-blog.html', '/2016/08/business-analytics-on-sales-data-of.html',
'/2017/12/gaussian-state-space-model-in-r-using.html', '/2017/09/assumptions-of-linear-regression.html',
'/2017/09/neural-network-forward-propagation-in.html', '/2018/06/lda-linear-discriminant-analysis-where.html',
'/2018/10/religious-demographics-of-india-in.html', '/2020/04/forecasting-total-deaths-from.html',
'/2019/11/automatic-ticket-resolution-for.html', '/2019/09/sentiment-analysis-using-nltk-and.html', '/2019/07/deep-learning-with-h2o-in-python.html',
'/2019/07/how-to-survive-in-data-science-and.html']
# change browser name below ( other than chrome)-
from time import sleep
url='https://machinelearningstories.blogspot.com'
for i in range(30):
item=random.choice(ls)
updated_url= url+item
print(updated_url)
webbrowser.get('chrome').open(updated_url)
sleep(5)
os.system("pkill -f Chrome")
Comments
Post a Comment