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

Selenium Python Element.click not working

$
0
0

I am new to Selenium and I am trying to mimic user actions on a site to download a csv file.

The "Get Data" button generates the link "download file in csv format" when I do it manually but it doesn't work with the automation.

I get an exception saying the link was not found.

Here are the things that i tried:

  • Adding explicit waits to have enough time for the page to load. Didn't work.

  • Adding explicit waits between click on get_data and before clicking on download link. Didn't work.

  • Tried different ways of the clicking the "get data" button ie

     i) get element by xpath and then click.
    
     ii) driver.wait_until(....    driver.find_element.. and not(@disabled). to make sure the button is    not disabled. didn't work    
     iii) Added asserts to make sure the    button is displayed and enabled.
    

** Here is anomaly which I don't understand. I went through the html file to understand how its working and there is javascript which validates the input data. So I guess the "get_data" wont work if the input data is incorrect.

So I tried to display the text after entering the symbol "SBIN" via sendkeys by

print(driver.find_element_by_id("symbol").text)

and this prints empty. Not sure if this is problem.

Would be really grateful for a solution as it would save me ton of manual effort is getting stock data.

Here is the website. https://www1.nseindia.com/products/content/equities/equities/eq_security.htm

Here is the python script for the webdriver.

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Test1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_1(self):
        driver = self.driver
        driver.get("https://www1.nseindia.com/products/content/equities/equities/eq_security.htm")
        driver.find_element_by_id("dataType").click()
        driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Get historical data for:'])[1]/following::option[3]").click()
        driver.find_element_by_id("symbol").click()
        driver.find_element_by_id("symbol").clear()
        driver.find_element_by_id("symbol").send_keys("SBIN")
        driver.find_element_by_id("series").click()
        Select(driver.find_element_by_id("series")).select_by_visible_text("EQ")
        driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Select series :'])[1]/following::option[15]").click()
        Select(driver.find_element_by_id("dateRange")).select_by_visible_text("24 Months")
        driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='For past:'])[1]/following::option[8]").click()
        driver.find_element_by_id("get").click()
        driver.find_element_by_link_text("Download file in csv format").click()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

Viewing all articles
Browse latest Browse all 98775

Trending Articles



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