'Python Selenium Script
Could you please help me to solve issue with my python selenium script, if fact I want to scrap some value from a dashboard, but I got some errors, please find attached my script and below a screenshot of my terminal outputs.
Best regards
The code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import pandas as pd
import time
def get_and_wait_element(browser, xpath, sec_wait=10):
WebDriverWait(browser, sec_wait).until(
EC.presence_of_all_elements_located((By.XPATH, xpath))
)
return browser.find_element(By.XPATH, xpath)
def get_and_wait_elements(browser, xpath, sec_wait=10):
WebDriverWait(browser, sec_wait).until(
EC.presence_of_all_elements_located((By.XPATH, xpath))
)
return browser.find_elements(By.XPATH, xpath)
def ini_browser():
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/99.0.4844.84 Safari/537.36"
opts = Options()
opts.add_argument("user-agent=" + USER_AGENT)
s=Service(ChromeDriverManager().install())
browser = webdriver.Chrome(service=s, options=opts)
return browser
browser = ini_browser()
# open the browser
browser.get('https://app.sales.rocks/login')
# login page
time.sleep(4)
email_text_box = get_and_wait_element(browser,
'/html/body/div[2]/div/div/div/div[1]/div[1]/div/div/div[2]/div/form/div[1]')
email_text_box.click()
email_text_box_input = get_and_wait_element(browser,
'/html/body/div[2]/div/div/div/div[1]/div[1]/div/div/div[2]/div/form/div[1]/div/input')
email_text_box_input.send_keys(EMAIL)
password_text_box = get_and_wait_element(browser,
'/html/body/div[2]/div/div/div/div[1]/div[1]/
div/div/div[2]/div/form/div[2]/td[1]/div/div/input')
password_text_box.click()
password_text_box_input = get_and_wait_element(browser,
'/html/body/div[2]/div/div/div/div[1]
/div[1]/div/div/div[2]/div/form/div[2]/td[1]/div/div/input')
password_text_box_input.send_keys(PASSWORD)
login_button = get_and_wait_element(browser,
'/html/body/div[2]/div/div/div/div[1]/div[1]/div/div/div[2]/div/form/div[4]/button[2]')
login_button.click()
time.sleep(7)
# skip page tutorial
browser.switch_to.frame('LOU_PLAYER_MAINFRAME')
get_and_wait_element(browser, '//*
[@id="__next"]/div/div/div/div/div/div/div/div[2]/div[2]/button').click()
time.sleep(4)
get_and_wait_element(browser, '/html/body/div/div/div/div/div/div/div[1]').click()
browser.switch_to.default_content()
time.sleep(4)
# click "Drip Campaigns"
get_and_wait_element(browser, '//*
[@id="app"]/div/div[1]/div/div/div/section/div[5]/div/span[1]/span[2]').click()
time.sleep(4)
# click "My Campaigns"
get_and_wait_element(browser, '//*[
@id="app"]/div/div[1]/div/div/div/section/div[5]/ul/li[2]/div/a/span[2]').click()
# extract data
time.sleep(4)
table_xpath = '//*[@id="campaign-list"]/div/div[1]/table/tbody/tr'
campaign_name_xpath = '/td[2]/span/div/div/p[1]'
connection_xpath = '/td[3]/span/p'
status_xpath = '/td[4]/span/div/p'
sent_xpath = '/td[6]/span/p'
opened_xpath = '/td[7]/span/div/p'
clicked_xpath = '/td[8]/span/div/p'
replied_xpath = '/td[9]/span/div/p'
opted_out_xpath = '/td[11]/span/div/p'
output_list = []
next_button = get_and_wait_element(browser, '//*[@id="campaign-
list"]/div/div[2]/div/div[2]/div/nav/button[2]')
while next_button.is_enabled():
table_row_count = len(get_and_wait_elements(browser, table_xpath))
for table_row in range(1, table_row_count+1):
# print(get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
opted_out_xpath).text)
output_list.append([
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
campaign_name_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
connection_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
status_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
sent_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
opened_xpath).text.replace("%", ''),
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
clicked_xpath).text.replace("%", ''),
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
replied_xpath).text.replace("%", ''),
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
opted_out_xpath).text.replace("%", ''),
])
next_button.click()
time.sleep(4)
# extract data from last page
time.sleep(4)
table_row_count = len(get_and_wait_elements(browser, table_xpath))
for table_row in range(1, table_row_count+1):
output_list.append([
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
campaign_name_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
connection_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
status_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' + sent_xpath).text,
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
opened_xpath).text.replace("%", ''),
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
clicked_xpath).text.replace("%", ''),
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
replied_xpath).text.replace("%", ''),
get_and_wait_element(browser, table_xpath + '[' + str(table_row) + ']' +
opted_out_xpath).text.replace("%", ''),
])
# save file
df = pd.DataFrame(output_list, columns=['CAMPAIGN NAME', 'CONNECTION', 'STATUS', 'SENT',
'OPENED %', 'CLICKED', 'REPLIED', 'OPTED OUT']) #List
pd.DataFrame(df).to_csv("MIO Sales.Rocks - email stats.csv", index=False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

