absolute noob here but I recently started a python automation project which checks the day of the week, and if it's a weekday it should open a browser with my college schedule for example, and if it's a weekend day it should open up different websites.
I use the brave browser as my default browser but I was wondering if it is possible to open up my regular browser profile when I run my code instead of the new instance where it does not have all my bookmarks and passwords?
Thanks in advance!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import datetime
day = datetime.datetime.now()
dag = day.weekday()
def Testing():
if dag >= 5:
return Weekend()
else:
return Weekday()
def Weekend():
options = Options()
options.add_argument("--window-size=1920,1080")
## options.add_argument("/Users/vadim/Library/Application Support/BraveSoftware/Brave-Browser")
options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
driver_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(options=options, executable_path=driver_path)
driver.get('https://outlook.live.com/mail/0/inbox')
Outlook_Aanmelden = driver.find_element_by_xpath('/html/body/header/div/aside/div/nav/ul/li[2]/a')
Outlook_Aanmelden.click()
Email_Field = driver.find_element_by_xpath('//*[#id="i0116"]')
Email_Field.send_keys('#live.com')
Outlook_Volgende = driver.find_element_by_xpath('//*[#id="idSIButton9"]')
Outlook_Volgende.click()
time.sleep(0.5)
Password_Field = driver.find_element_by_xpath('//*[#id="i0118"]')
Password_Field.send_keys('pass')
Password_Field.send_keys(Keys.ENTER)
Inlog_Outlook = driver.find_element_by_xpath('//*[#id="idSIButton9"]')
Inlog_Outlook.click()
driver.execute_script("window.open('https://youtube.com');")
def Weekday():
options = Options()
options.add_argument("--window-size=1920,1080")
## options.add_argument("/Users/vadim/Library/Application Support/BraveSoftware/Brave-Browser")
options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
driver_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(options=options, executable_path=driver_path)
driver.get('https://outlook.live.com/mail/0/inbox')
Outlook_Aanmelden = driver.find_element_by_xpath('/html/body/header/div/aside/div/nav/ul/li[2]/a')
Outlook_Aanmelden.click()
Email_Field = driver.find_element_by_xpath('//*[#id="i0116"]')
Email_Field.send_keys('#live.com')
Outlook_Volgende = driver.find_element_by_xpath('//*[#id="idSIButton9"]')
Outlook_Volgende.click()
time.sleep(0.5)
Password_Field = driver.find_element_by_xpath('//*[#id="i0118"]')
Password_Field.send_keys('pass')
Password_Field.send_keys(Keys.ENTER)
Inlog_Outlook = driver.find_element_by_xpath('//*[#id="idSIButton9"]')
Inlog_Outlook.click()
driver.execute_script("window.open('https://youtube.com');")
Testing()
I recommend using webbrowser instead of selenium, it's part of the python standard library so you should already have it.
import webbrowser
webbrowser.open('https://stackoverflow.com/')
Here's the doc if you wan't to play around with it.
Related
I'm trying to use Selenium to click on multiple links one each a time to download multiple CSV files, the problem here is the selenium makes the donwload of about few csv files but in the middle of the loop it stops working, crashes the Browser accusin that don't have internet and close the driver. I already put the chromedriver.exe in the same folder and put the path but it still not working.
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
import os
import re
new_directory = r"Z:\BI_Database_teste"
for document in os.listdir(new_directory):
os.remove(os.path.join(new_directory, document))
url = 'myPersonalURL'
chrome_options = Options()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome('chromedriver.exe', options=chrome_options)
params = {'behavior': 'allow', 'downloadPath': new_directory}
browser.execute_cdp_cmd('Page.setDownloadBehavior', params)
browser.get(url)
time.sleep(2)
input_email = browser.find_element(By.ID, 'email')
input_email.send_keys('myEmail')
input_password = browser.find_element(By.ID, 'password')
input_password.send_keys('myPassword')
input_password.submit()
input_question = browser.find_element(By.XPATH, '/html/body/div[2]/div[2]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]').text
answer_field = browser.find_element(By.XPATH, '/html/body/div[2]/div[2]/form/table/tbody/tr[3]/td/table/tbody/tr[3]/td[2]/input')
if input_question == 'question':
answer_field.send_keys('answer')
elif input_question == 'question':
answer_field.send_keys('answer')
else:
answer_field.send_keys('answer')
time.sleep(2)
answer_field.submit()
time.sleep(4)
links = browser.find_elements(By.LINK_TEXT, 'Export (CSV)')
links_len = len(links)
print(str(links_len) + ' BI Databases to Download')
list_count = 0
for link in range(list_count, links_len):
time.sleep(2)
links[list_count].click()
list_count = list_count + 1
print(str(list_count) + ' BI Databases downloaded')
browser.quit()
for file in os.listdir(new_directory):
if file.startswith("PBI"):
try:
os.rename(os.path.join(new_directory, file), os.path.join(new_directory, re.sub('[0-9]', '', file)))
except:
pass
print('BI Databases Download Successfully!')```
Could someone help me to find out why the webdriver stops working in the middle of the loop?
If you think that for some reason the driver isn't loaded correctly then you can download the webdriver on runtime from your code, this might help,
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chrome = webdriver.Chrome(ChromeDriverManager().install(), options=options)
without much further context, the question cannot be answered or reproduced.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
import configparser
from datetime import datetime
parser = configparser.RawConfigParser()
parser.read('config.ini')
url= parser['PROPERTIES']['URL']
END_DATE = datetime.strptime(parser['DATE']['END'], '%Y-%m-%d')
START_DATE=datetime.strptime(parser['DATE']['START'],'%Y-%m-%d')
# Setting up driver options
options = webdriver.ChromeOptions()
# Setting up Path to chromedriver executable file
CHROMEDRIVER_PATH =r'C:\Users\HP\Desktop\INTERNSHIP\influenster\chromedriver.exe'
# Adding options
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
# Setting up chrome service
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
# Establishing Chrom web driver using set services and options
driver = webdriver.Chrome(service=service, options=options)
driver.get(url)
reviews=driver.find_elements_by_xpath('//*[#id="app-base"]/div[1]/div[4]/div[1]/div[1]/div[3]')
count=0
item_list = []
for review in reviews:
item={
'username': review.find_element_by_xpath(".//a[contains(#class,'name')]").text,
}
item_list.append(item)
print(item_list)
OUTPUT IS JUST ONE NAME AND NOT ALL
I need to scrape all the reviews from https://www.influenster.com/reviews/loreal-paris-elvive-extraordinary-oil-deep-nourishing-shampoo-and-conditioner-set-126-fl-oz. Even I am running a loop I am getting only one username. Please help me out
You getting only 1 review because XPath locator you are using //*[#id="app-base"]/div[1]/div[4]/div[1]/div[1]/div[3] returns only 1 element, so your for loop is performed only once.
You can improve this code by improving that XPath locator.
Also you need to close the cookies banner there.
Also you need to add a wait to waif for elements to be clickable / visible before you accessing them, as following.
This should work better:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import configparser
from datetime import datetime
parser = configparser.RawConfigParser()
parser.read('config.ini')
url= parser['PROPERTIES']['URL']
END_DATE = datetime.strptime(parser['DATE']['END'], '%Y-%m-%d')
START_DATE=datetime.strptime(parser['DATE']['START'],'%Y-%m-%d')
# Setting up driver options
options = webdriver.ChromeOptions()
# Setting up Path to chromedriver executable file
CHROMEDRIVER_PATH =r'C:\Users\HP\Desktop\INTERNSHIP\influenster\chromedriver.exe'
# Adding options
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
# Setting up chrome service
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
# Establishing Chrom web driver using set services and options
driver = webdriver.Chrome(service=service, options=options)
wait = WebDriverWait(driver, 20)
driver.get(url)
# The 2 lines below is what I actually added here + necessary imports
# and `wait` object initialization
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
reviews = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".conversations-left .item")))
count=0
item_list = []
for review in reviews:
item={
'username': review.find_element_by_xpath(".//a[contains(#class,'name')]").text,
}
item_list.append(item)
print(item_list)
I have a code for making temp mail automatically but I have a problem.
This is code:
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://temp-mail.org/")
time.sleep(10)
browser.close()
The link opens correctly but I can't pass cloudflare.
Also, I see some errors on my console:
Thanks...
Try adding user agent argument in chrome options and set user agent to any value
ops = Options()
ua='me'
ops.add_argument('--user-agent=%s' % ua)
driver=uc.Chrome(executable_path=r"C:\chromedriver.exe",chrome_options=ops)
Alternatively try using undetected-chromedriver
import undetected_chromedriver as uc
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = uc.Chrome(options=options)
driver.get("https://temp-mail.org/")
So basically, I have a list of droplists that I need to interact with.
I know how to interact with the first droplist.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
options = Options()
options.headless = False
driver = webdriver.Chrome(PATH,options = options)
driver.set_window_size(1920,1080)
driver.get("https://www.compraensanjuan.com")
time.sleep(3)
link = driver.find_element_by_link_text("Mi cuenta")
link.click()
time.sleep(3)
email = driver.find_element_by_name("email")
email.send_keys("yourmail")
password = driver.find_element_by_name("clave")
password.send_keys("Yourpassword")
password.send_keys(Keys.RETURN)
time.sleep(3)
drp = Select(driver.find_element_by_id("acciones"))
drp.select_by_visible_text("Actualizar")
driver.back()
But how do I repeat the same action for all the following droplists??
You may be able to get a list of the options, and then iterate over them. Something like:
options = driver.find_element_by_name('DropdownElement')
options = options.text.split('\n')
for opt in options:
driver.find_element_by_xpath("//select[#name='DropdownElement']/option[text()='"+opt+"']").click()
i'm currently trying to automate the task of clicking different download links from this website:
https://www.theice.com/clear-us/risk-management#margin-rates
in this page, i first have to click the "Download ICE Risk Model array files" header which gives me 2 dropdowns from which i want to first click the "Final" link which downloads a csv file for each month of each available year.
Currently, both the dropdowns change due to hidden dropdown menus above, i have first tried to make them visible which was successful as well as changing year in it using selenium click,
The problem arising is that i'm not able to click the "Final" link in the csv section but it just clicks the "
Intercontinental Exchange"
button in the footer and navigates to a new page.
Is there anyway to get this task done ?
As well as is it possible to change the download location to the current directory where the .py script is ?
This is the python code so far, i currently removed the headless part to see what's going on :
from bs4 import BeautifulSoup
import requests
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
import os
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--log-level=3")
chrome_path = which("chromedriver")
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1366, 768)
driver.get("https://www.theice.com/clear-us/risk-management#margin-rates")
main_button = driver.find_element_by_xpath('//h4[#class="collapsible-section-header"]')
main_button.click()
time.sleep(5)
driver.execute_script("document.getElementById('icus-ice-form-year').style.display = 'block';")
driver.execute_script("document.getElementById('icus-ice-form-month').style.display = 'block';")
time.sleep(1)
dropdown_1 = Select(driver.find_element_by_xpath('//select[#id="icus-ice-form-year"]'))
dropdown_2 = Select(driver.find_element_by_xpath('//select[#id="icus-ice-form-year"]'))
main_table_div = driver.find_element_by_xpath('//div[#id="icus-ice-riskarraytable"]')
main_table = main_table_div.find_element_by_xpath('//table[#class="table table-data"]')
for opt in dropdown_1.options:
opt.click()
for opt2 in dropdown_2.options:
opt2.click()
time.sleep(3)
download_links_1 = main_table.find_elements_by_xpath('//td[#class="table-partitioned"]')
for dow in download_links_1:
try:
temp_dow = dow.find_element_by_xpath('//a')
temp_dow.click()
time.sleep(4)
except:
pass
This should switch the downloads to the current working directory and print all the early csvs. change '//[#id="icus-ice-riskarraytable"]/table/tbody/tr[{0}]/td[2]/a' to '//[#id="icus-ice-riskarraytable"]/table/tbody/tr[{0}]/td[3]/a' for the other final csv.
options = Options()
currentDirectory = os.getcwd()
prefs = {
"download.default_directory": currentDirectory,
"download.prompt_for_download": False
}
#print(currentDirectory)
options.add_experimental_option("prefs", prefs)
driver.get("https://www.theice.com/clear-us/risk-management#margin-rates")
driver.implicitly_wait(5)
main_button = driver.find_element_by_xpath('//h4[#class="collapsible-section-header"]')
main_button.click()
driver.implicitly_wait(5)
driver.execute_script("document.getElementById('icus-ice-form-year').style.display = 'block';")
driver.execute_script("document.getElementById('icus-ice-form-month').style.display = 'block';")
driver.implicitly_wait(5)
drop1length=len(driver.find_elements_by_xpath('//select[#id="icus-ice-form-year"]/option'))
#print(drop1length)
for i in range(1,drop1length-1):
drop1=Select(driver.find_element_by_xpath('//select[#id="icus-ice-form-year"]'))
drop1.select_by_index(i)
drop2length=len(driver.find_elements_by_xpath('//select[#id="icus-ice-form-month"]/option'))
#print(drop2length)
for j in range(1,drop2length-1):
drop2=Select(driver.find_element_by_xpath('//select[#id="icus-ice-form-month"]'))
driver.implicitly_wait(5)
drop2.select_by_index(j)
download_links_length = len(driver.find_elements_by_xpath('//*[#id="icus-ice-riskarraytable"]/table/tbody/tr/td[2]/a'))
#print(download_links_length)
for dow in range(1,download_links_length-1):
try:
element = driver.find_element_by_xpath('//*[#id="icus-ice-riskarraytable"]/table/tbody/tr[{0}]/td[2]/a'.format(str(dow)))
driver.implicitly_wait(5)
driver.execute_script("arguments[0].click();", element)
driver.switch_to.window(driver.window_handles[0])
except Exception as e:
print(e)
Import these
import os
from selenium.webdriver.chrome.options import Options