from bs4 import BeautifulSoup
import requests
from contextlib import closing
from selenium.webdriver import Chrome # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
import selenium
import os
import re
import csv
import sys
pPortalBS = requests.get("https://sapeadfs.sapient.com/adfs/ls?wa=wsignin1.0&wtrealm=urn%3asamlcolo%3apeopleportal&wctx=https%3a%2f%2fpeopleportal2.sapient.com%2fmy%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F")
pPortal = BeautifulSoup(pPortalBS.content,"html5lib")
#print pPortal
chromedriver = "C:/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
#driver = webdriver.Chrome(chromedriver)
#12,18,30,39,48,76,78
i=0
logged_in_flag = 0
with closing(Chrome(chromedriver)) as driver:
while(1):
try:
driver.get("https://sapeadfs.sapient.com/adfs/ls?wa=wsignin1.0&wtrealm=urn%3asamlcolo%3apeopleportal&wctx=https%3a%2f%2fpeopleportal2.sapient.com%2fmy%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F")
'''
try:
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "hrdSelection"))
)
finally:
driver.quit()
'''
#button = driver.find_element_by_id('idp')
page_source = driver.page_source
#page_source
button = driver.find_elements_by_xpath(".//*/div[@class='idpDescription float']")
print button
button[1].click()
# wait for the page to load
element = WebDriverWait(driver, 10).until(
EC.invisibility_of_element_located((By.ID, "hrdSelection"))
)
# store it to string variable
page_source = driver.page_source
#logging in
if(logged_in_flag ==0):
try:
usernameInput = driver.find_element_by_name("UserName")
usernameInput.send_keys("shumayan@publicisgroupe.net")
passwordInput = driver.find_element_by_id("passwordInput")
passwordInput.send_keys("Xendering@678")
driver.find_element_by_id("submitButton").click()
logged_in_flag = 1
except:
logged_in_flag = 1
#people portal home
with closing(open("Go West Resource List.csv", "r")) as ifile:
with closing(open("Go West Resource List Out Detailed Missing.csv", "a")) as ofile:
fileWriter = csv.writer(ofile)
reader = csv.reader(ifile)
rownum = 0
print reader
for row in reader:
# Save header row.
#print rownum
newRow = row
if rownum == 0:
header = row
elif rownum in range(1,i+1):
rownum = rownum +1
continue;
else:
colnum = 0
for col in row:
if(colnum == 1):
if int(col) in [107590,21471,54226,130525,127212,125423,120988,48426,46183,59615,89696,88052,120465,59615,56277,119013,39460,46183,22920,79590,43848,25908,52556,60466,107439,100470,107439,105611,78666,78666,82833,59615,101430,59615,117872,82833,20332,117872,22920,23703,84858,59615,59615,63648,185,105611,121585,120043,107439,79564,22116,46183,91066,51892,46183,52591,77173,77762,91238,1018,129661,1018,113662,49410,24560,127848,46982,46183,133378,46982,57335,46982,46982,46982,52591,46982,77173,46982,46982,46982,46982,46982,46982,46982,46982,90166,50615,46183,46183,21702,115801,46183,23703,23703,219,49410,132,1127,339,46183,24560,1607,23703,665,78666,117872,43544,21235,1127,110479,99,219,46738,82002,182,23531,62022,128329,21668,62924,22738,46183,219,20923,54262,21702,118739,178,49220,23703,99,61075,660,77762,117872,21702,70423,28588,21795,101430,46183,83720,70927,33632,82334,41456,50892,25908,109358,117362,32474,90166,24560,31523,48632,90166,88052,49410,81322,38700,59615]:
#1049
print "ok"
else:
continue
#try:
print (header[colnum], col)
#col = 133916
element = WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.ID, "txtSearchPeople"))
)
searchbox = driver.find_element_by_id("txtSearchPeople")
#oidInput = driver.find_element_by_id("passwordInput")
searchbox.send_keys(col)
searchbutton = driver.find_element_by_id("lbtnGoPeople")
searchbutton.click()
person = driver.find_element_by_id("NameFieldLink").click()
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "dvMyInformationContent"))
)
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "dvAssignmentDetails"))
)
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "tblPhoneNumberAddEdit"))
)
page_source = driver.page_source
pPortal = BeautifulSoup(page_source, "html5lib")
informationWindow = pPortal.find("div",{"id":"dvMyInformationContent"})
#print informationWindow.text
informationWindowRows = informationWindow.findAll("div",{"class":"row"})
startDate = ""
IM=""
for informationWindowRow in informationWindowRows:
informationWindowRowData = informationWindowRow.findAll("div")
#print informationWindowRowData
foundDateFlag = 0
foundIMflag = 0
foundEmailFlag = 0
IM_visited = 0
SD_visited = 0
EM_visited = 0
for data in informationWindowRowData:
#print data.text
if(data.text == "IM"):
foundIMflag = 1
continue;
if(foundIMflag == 1 and IM_visited == 0):
IM = data.text
newRow.append(IM)
IM_visited = 1
if(data.text == "Start Date"):
foundDateFlag = 1
continue;
if(foundDateFlag == 1 and SD_visited == 0):
startDate = data.text
newRow.append(startDate)
SD_visited = 1
if(data.text == "Email"):
foundEmailFlag = 1
continue;
if(foundEmailFlag == 1 and EM_visited == 0):
email = data.text
newRow.append(email)
EM_visited = 1
if(foundDateFlag ==1 and foundIMflag == 1 and foundEmailflag == 1):
break;
titleBS = pPortal.find("div",{"class":"myprofile-name"})
titleSpans = titleBS.findAll("span")
title = titleSpans[-1].text
newRow.append(title)
home_location = ""
current_location = ""
employee_category = ""
comp_approver = ""
comp_communicator = ""
people_manager = ""
assignmentDetails = pPortal.find("div",{"id":"dvAssignmentDetails"})
detailsDivs = assignmentDetails.findAll("div",{"class":"table tblBorder"})
for detailDiv in detailsDivs:
rows = detailDiv.findAll("div",{"class":"row"})
for row in rows:
divs = row.findAll("div")
if(divs[0].text == "Home Location"):
home_location = divs[1].text
print home_location
if(divs[0].text == "Current Location"):
current_location = divs[1].text
if(divs[0].text == "Employee Category"):
employee_category = divs[1].text
if(divs[0].text == "Comp Approver"):
comp_approver = divs[1].text
if(divs[0].text == "Comp Communicator"):
comp_communicator = divs[1].text
if(divs[0].text == "People Manager"):
people_manager = divs[1].text
newRow.append(home_location)
newRow.append(current_location)
newRow.append(employee_category)
newRow.append(people_manager)
newRow.append(comp_approver)
newRow.append(comp_communicator)
phoneWindow = pPortal.find("table",{"id":"tblPhoneNumberAddEdit"})
details = phoneWindow.find("tbody",{})
trs = details.findAll("tr")
phoneNos = []
for tr in trs:
td = tr.findAll("td")
phoneNos.append(str(td[0].text))
newRow.append(phoneNos)
fileWriter.writerow(newRow)
i=i+1
print "success "+str(i)
#except:
# continue;
colnum = colnum + 1
rownum = rownum + 1
print "end"
break;
except selenium.common.exceptions.NoSuchElementException, e:
print "fail "+str(i)
print str(e)
i=i+1
continue
except Exception, e:
print "fail "+str(i)
print str(e)
i=i+1
logged_in_flag = 1
continue