Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98800

Button click works inconsistently (hangs) on Chrome Webdriver while executing from Python Selenium

$
0
0

I am using Selenium to automate a few browser actions on a particular website and I am using the below set of tools to achieve it.

  • Python 3.8
  • Selenium
  • Chrome Web Driver 79.0
  • Chrome 79.0

The tasks that I do are just fill up a form and then click on the submit button in the form. And this works most of the time except that sometimes it just won't no matter what! The filling up of form is very smooth and then, when the click on the submit button happens then Chrome just hangs there forever. There's no error on the console whatsoever and googling this issue I see this is such a common occurrence and almost all of the solution out there are just workarounds and not actual fixes. And I have tried almost all of them but to no avail. There's an issue on the selenium GitHub page as well which the maintainers weren't interested in much and closed it. How do I even go about resolving this issue. At this moment I am out of ideas actually. Any help would be appreciated. Thanks.

Below is the source code that I am trying to execute.

import time
from selenium import webdriver
import os
import csv
from datetime import datetime

url = 'https://www.nseindia.com/products/content/equities/equities/eq_security.htm'
xpath_get_data_button = '//*[@id="get"]'
xpath_download_link = '/html/body/div[2]/div[3]/div[2]/div[1]/div[3]/div/div[3]/div[1]/span[2]/a'
nse_list_file = 'nse_list.csv'
wait_time = 5
wait_time_long = 10
start_year = 2000
stop_year = 2019
curr_year = start_year

browser = webdriver.Chrome("E:/software/chromedriver_win32/chromedriver.exe")
browser.get(url)

time.sleep(wait_time)

with open(nse_list_file, 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        scrip = row[0]
        year_registered = datetime.strptime(row[1], '%d-%m-%Y').year

        if year_registered > start_year:
            curr_year = year_registered
        else:
            curr_year = start_year

        try:
            browser.find_element_by_class_name('reporttitle1').clear()
            browser.find_element_by_class_name('reporttitle1').send_keys(scrip)

            browser.find_element_by_id('rdDateToDate').click()

            while curr_year <= stop_year:
                from_date = '01-01-' + str(curr_year)
                to_date = '31-12-' + str(curr_year)

                browser.find_element_by_id('fromDate').clear()
                browser.find_element_by_id('fromDate').send_keys(from_date)

                browser.find_element_by_id('toDate').clear()
                browser.find_element_by_id('toDate').send_keys(to_date)

                time.sleep(wait_time)

                browser.find_element_by_xpath(xpath_get_data_button).click()

                time.sleep(wait_time_long)
                download_link_element = browser.find_element_by_xpath(xpath_download_link).click()

                curr_year = curr_year + 1

        except Exception as ex:
            print('Could not find download link')
            print(str(ex))

        if os.path.isfile("stop_loading"):
            break

browser.quit()

print('DONE')

Viewing all articles
Browse latest Browse all 98800

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>