Reputation: 195
I recently started learning python. I started working on a small automation task for which I've chosen selenium. The task is, a .NET program will run and a profiler(I'm using Stackify Prefix) will log all the calls as below.
I've to loop through all the calls on left hand side and to click each one. Which will open a window on the right side with all the data. Now, I need data from the right hand side under "Request Started". Beside "Request Started", I've to find the plus symbol, click it and then fetch another div which contains the method name. Like below
While fetching the elements to click the plus symbol, the code is failing. Below is the code.
from selenium import webdriver driver = webdriver.Chrome() driver.get("http://localhost:2012") #colIdentList = driver.find_element_by_class_name('colIdent') for result in driver.find_elements_by_class_name('colIdent'): result.click() #wait = WebDriverWait(driver, 5) for plus in driver.find_elements_by_class_name('collapser'): plus[0].click() for methodDiv in driver.find_elements_by_class_name('willnotrender'): value = methodDiv[0].text driver.close()
'collapser' is the class for that div which resides under multiple div and td. I'm pasting XPATH for that component as a reference. But the XPATH is not always same and is different. I found two XPATHs for the element.
XPATH : 1. /html/body/div[3]/div/ul/li2/div2/div[3]/div 2. /html/body/div[3]/div/ul/li2/div/div[3]/div
Upvotes: 0
Views: 667
Reputation: 195
After some searches and a little help from my friend, Mahesh Kosuri I was able to complete the code. I was missing the fact that the webpage is having multiple frames. So, selenium was unable to find the element I was searching.
Below is the code which did the task for me.
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import pandas as pd timetook,methodname,dbcalls=[],[],[] runnum,sheetname,counter=3,7,0 driver = webdriver.Chrome() driver.get("http://localhost:2012/") result in driver.find_elements_by_class_name('colIdent'): try: result.click() counter+=1 if counter>8: driver.execute_script("document.querySelector('body > div.trace-list-area').scrollBy(0,75)") except: driver.execute_script("document.querySelector('body > div.trace-list-area').scrollBy(0,100)") result.click() driver.switch_to_frame(driver.find_element_by_id('trace-frame')) wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body > div.scroll-area > div > ul > li:nth-child(2) > div > div.flex-horizontal > div > button'))) timetook.append(driver.find_elements_by_css_selector('body > div.heading-trace-panel > div.tracebar-details > div > table > tbody > tr > td:nth-child(2) > div.tracebar-value')[0].text) driver.find_elements_by_css_selector('body > div.scroll-area > div > ul > li:nth-child(2) > div > div.flex-horizontal > div > button')[0].click() methodname.append(driver.find_elements_by_css_selector('#ui_0_stacks > li.logblock.backdrop-timing.logblock-message > div > div.flex-horizontal > div.prettyprint-container')[0].text) try: dbcalls.append(driver.find_elements_by_css_selector('body > div.heading-trace-panel > div.tracebar-details > div > table > tbody > tr > td.stackNumbers > a > span')[0].get_attribute('data-badge')) except: dbcalls.append(0) driver.switch_to_default_content() df=pd.DataFrame() df["TimeTook"] = timetook df["methodName"]=methodname df["DBCalls"]=dbcalls df.to_excel("Timing Report "+str(runnum)+"_"+str(sheetname)+".xlsx") driver.close()
Upvotes: 1