1

I'm running a script to login a website and extract information to excel. It used to run smoothly and fast, but now it is just SLOW and SLOW. It took 15 seconds to run .click() method while it use to take only 2 Seconds. I'm wondering is there any thing wrong with my codes that is producing such inefficient results? Also, my excel file went corrupted and unable to open after running the script. Any help will be appreciated.

from selenium import webdriver
import XLUtlis
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome("C:\Users\XUPJ21WJH\Desktop\Testing\Drivers\chromedriver.exe")
driver.get("https://cmis2.cat.com/cmis_tc/Main.jsp?selectedURL=/CoreInquiry")
driver.maximize_window()
driver.find_element_by_name("cwsUID").send_keys("J1W")
driver.find_element_by_name("cwsPwd").send_keys("")
driver.find_element_by_id("submitButton").send_keys(Keys.ENTER)

path = "C://Users/XUPJ21WJH/Desktop/test.xlsx"

row = XLUtlis.getRowCount(path, 'Sheet1')

for r in range(2, row + 1):
    j19r = XLUtlis.readData(path, 'Sheet1', r, 1)
    driver.find_element_by_name("CcrNo").send_keys(j19r)
    driver.find_element_by_name("Search").click()
    driver.find_element_by_css_selector("#main_screen > form > table:nth-child(5) > tbody > tr.tableBodyOdd > td:nth-child(1) > table > tbody > tr > td:nth-child(1) > a > img").click()
    driver.find_element_by_xpath("//a[@href='Main.jsp?selectedURL=/CCRViewCredit']").click()
    number = driver.find_element_by_xpath("//tr[@class = 'tableBodyOdd']/td[position() = last() -1]").text
    XLUtlis.writeData(path, 'Sheet1', r, 3, number)
    driver.find_element_by_css_selector("#main_screen > form > div:nth-child(5) > table:nth-child(2) > tbody > tr > td > p > input[type=submit]:nth-child(1)").click()
    driver.find_element_by_css_selector("#main_screen > div > form > div:nth-child(6) > table:nth-child(10) > tbody > tr > td > input[type=SUBMIT]:nth-child(3)").click()
    driver.find_element_by_css_selector("#main_screen > form > center:nth-child(7) > input").click()
JiaHao Wang
  • 101
  • 3
  • 13
  • Try to use fast selectors for all the elements you are accessing. I can see that in provided code you are using name and ID to find few elements. There also it would be nice if you use fast selectors like XPATH or CSS selector . – Rohit Gawas Aug 28 '19 at 01:42
  • As you are not using any types of wait (I assume the same for rest of your code that you have not provided here) , I think your website is slow and it is taking more time to load the elements. Due to this your selenium script is also slowing down. – Rohit Gawas Aug 28 '19 at 01:44
  • One more think you can do is , you can try different method to write data in excel. Try openpyxl for writing data in excel. You can refer following link for understanding usage of openpyxl https://openpyxl.readthedocs.io/en/stable/tutorial.html – Rohit Gawas Aug 28 '19 at 01:49
  • I've actually notice that the `.click()` method only runs after the website is fully loaded. – JiaHao Wang Aug 28 '19 at 02:22
  • So this solves your query for slowing down of the script. Have you tried Openpyxl for writing data in Excel ? – Rohit Gawas Aug 28 '19 at 10:27
  • yes i was always using Openpyxl, i solved the problem for excel as well. Thank you for your kind help. – JiaHao Wang Aug 29 '19 at 03:07

0 Answers0