2020-10-30

Why a click function of a web page element doesn't work (using Selenium module of Python)?

I am trying to automate a task which can save me thousands of clicks.

I searched a bit for available modules in Python and I have selected to work with Selenium.

I installed Firefox drivers and did some progress but I am stuck for a long time. I finally gave up, opened a Stack Overflow account and wanted to bring this problem into this helpful medium.

My code can successfully do some clicks, but I could not make the code click a button like element. I have to click such items so that page opens some new elements. (Hopefully I am going to click on these new elements to save some excels files automatically).

Here is the part which works as expected:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

browser = webdriver.Firefox()
url =  "https://www.tbb.org.tr/tr/bankacilik/banka-ve-sektor-bilgileri/istatistiki-raporlar/59"
browser.get(url)
time.sleep(2)

bank_reports_element_xpath = "//*[@title=  'Tüm Raporlar']"
bank_reports_element = browser.find_element_by_xpath(bank_reports_element_xpath)
bank_reports_element.click()
time.sleep(2)

second_item = "//div[@data-catid = '52']"
finance_tables_element = browser.find_element_by_xpath(second_item)
finance_tables_element.click()

years_item = "//div[@class = 'years']"
years_element = finance_tables_element.find_element_by_xpath(years_item)
year_elements = years_element.find_elements_by_xpath("a")

There I would like to click on the years. a screenshot of the years that I can't click using Selenium

As an example, I get the element related to year 2004.

year2004 = year_elements[2]

Issuing a year2004.click() command gives an ElementNotInteractableException exception.

year2004.click()  # ElementNotInteractableException: Element could not be scrolled into view

Based on searching similar posts, I tried the following (executing the click via javascript). I got no error but it does not do anything. When I click the year2004 button with mouse, a list pops-up in the page. But when I run the below code, no list pops up in the page.

browser.implicitly_wait(4)
browser.execute_script("arguments[0].scrollIntoView();", year2004)
browser.execute_script("arguments[0].click()", year2004)

I tried also the following code. I get "TypeError: rect is undefined"

browser.implicitly_wait(4)
action = ActionChains(browser)
action.move_to_element(year2004)       # gives: TypeError: rect is undefined
action.click(on_element = year2004)    # gives: TypeError: rect is undefined
action.perform()

I tried using Chrome Driver and got similar results. The errors definitions were not exactly the same. Instead of the above "rect is undefined" error, Chrome error is like: "Javascript error: Failed to execute 'elementsFromPoints' on 'Document': Provided double value is non-finite"

I don't know if it is relevant but year2004.location dictionary has a 0 value for "x".

I would appreciate any answer,

Keep safe



from Recent Questions - Stack Overflow https://ift.tt/3jJhr4G
https://ift.tt/eA8V8J

No comments:

Post a Comment