ElementNotInteractable Exception: Message: Element <a href=> could not be scrolled into view - python-3.x

I am trying to click and download "Real Sector" on the following link:
http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm
Here is what I have tried:
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
driver.find_element_by_css_selector("a[href= 'Chap-2.pdf']").click()
except NoSuchElementException:
pass
But it gives following error:
ElementNotInteractableException: Message: Element could not be scrolled into view
How can I resolve this?

You need to apply Explicit Wait and wait till the element is present on the page and then you can click on it.
You can also scroll to the element first and then click on it.
You can do it like:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[#href='Chap-2.pdf']")))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
except NoSuchElementException:
pass
OR
You can directly click on the element using java script click like:
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[#href='Chap-2.pdf']")))
driver.execute_script("arguments[0].click();", element)
except NoSuchElementException:
pass

Try to wait use element_to_be_clickable
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href= 'Chap-2.pdf']")))
element.click()

To click on "Real Sector" link Induce WebDriverWait() and element_to_be_clickable() and following xpath option.
driver = webdriver.Chrome()
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Real Sector')]"))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Browser snapshot after clicking.

Related

Message;no such element(selenium xpath)

I have a problem with selenium find_by_xpath
This is my xpath : //input [#type='text']
And I get this error:
enter image description here
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input [#type='text']"}
from selenium import webdriver
path="C:\Program Files (x86)\chromedriver.exe"
username="123456"
password="123456"
url="https://www.foxesscloud.com/login"
driver=webdriver.Chrome(path)
driver.get(url)
driver.find_element_by_xpath("//input [#type='text']").send_keys("Pies123")
print("done")
print("Log")
It is because of Elements are not rendered properly and you are trying to interact with them. Please use Explicit wait to handle this situation and error.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.foxesscloud.com/login")
wait.until(EC.visibility_of_element_located((By.XPATH, "//input [#type='text']"))).send_keys('Pies123')
wait.until(EC.visibility_of_element_located((By.XPATH, "//input [#type='password']"))).send_keys('Your password here')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-click"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Scroll to right in Tradingview chart using python

I am trying to scroll right using selenium python in TradingView
My Code:
driver.find_element_by_xpath("//div[#class='control-bar__btn control-bar__btn--move-right apply-common-tooltip']").click()
It throws an error:
selenium.common.exceptions.ElementNotInteractableException: Message:
element not interactable
Try the below code -
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 60)
action = ActionChains(driver)
driver.maximize_window()
driver.get('https://www.tradingview.com/chart/')
try:
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#class,\"closeIcon\")]"))).click()
except:
pass
RightScroll = wait.until(EC.presence_of_element_located((By.XPATH,"//div[contains(#class,\"btn--move-right\")]")))
for i in range(10):
time.sleep(2)
print(i)
action.move_to_element(RightScroll).click().perform()
If it solves your problem then please mark it as answer. Do let me know if you have any query.
There is a message window displayed when you first load the page. You have to close it first (to get the right arrow container visible) if you are not doing it by
driver.findElement(By.xpath(".//span[contains(#class,'closeIcon')]")).click();
driver.findElement(By.xpath(".//td[#class='chart-markup-table time-axis']")).click();//To get the right arrow container visible
driver.findElement(By.xpath(".//div[#class='control-bar__btn control-bar__btn--move-right apply-common-tooltip']")).click();

How to click element using selenium web driver(Python)

I am trying to click an element using selenium chromedriver by the Use of ID of an element to click.
I want to Click Year '2020" from the following webpage: 'https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan'
I tried with the below code.
driver = webdriver.Chrome(executable_path=ChromeDriver_Path, options = options)
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
Id = "ctl00_ContentPlaceHolder1_gvMajorIncident_ct123_lbtnYear" ### Id of an Element 2020
wait = WebDriverWait(driver, 20) ##Wait for 20 seconds
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
time.sleep(10)
but unfortunately this gives an Error as below:
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
File "C:\Users\Pavan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Please anyone help me on this... Thanks;
I don't know if your imports were correct. Also, your code doesn't scroll to the bottom of the page. Use this.
import os
from selenium.webdriver.support import expected_conditions as EC
import time
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
driver = webdriver.Chrome(executable_path='driver path')
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
driver.maximize_window()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
element = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="ctl00_ContentPlaceHolder1_gvMajorIncident_ctl23_lbtnYear"]')))
element.click()

Python selenium issue locating a dropdown element

I'm trying to extract past one year prices.
Unable to get the XPath for the drop down.
Here's my recent code:
element1 = driver.find_element_by_xpath("""//button[#title="1 year"]""")
element2 = driver.find_element_by_xpath("""//*[#id="chartmenu"]/li/a""")
hoverover = ActionChains(driver).move_to_element(element1).move_to_element(
element2).click().perform()
Where am I wrong here? Please help!
To click() on the dropdown element with text as Download Price Index CSV you need you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.deribit.com/prinx_chart")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.btn-group.btn-group-sm button[title='1 year']"))).click()
download_element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.highcharts-container ")))
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.dropdown-toggle")))).perform()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul#chartmenu a[href*='PrChart'] span[data-i18n*='price_index_csv_download']"))).click()
Browser Snapshot:
Wait and check success login, than try to download. Here improved code:
driver.find_element_by_xpath("//input[#type = 'email']").send_keys(************)
driver.find_element_by_xpath("//input[#type = 'password']").send_keys(*********)
driver.find_element_by_name("go").click()
# wait until success login, by waiting some element on home page
driver.get('https://www.deribit.com/prinx_chart')
driver.find_element_by_xpath("//button[#title='1 year']").click()
driver.find_element_by_css_selector("span[data-i18n='app.price_index_csv_download']").click()

Python Selenium Vanguard: NoSuchElementException: no such element: Unable to locate element with Selenium and Python

I am trying to download history data and click on link to historical data. However even though the Xcode is correct I get this error:
NoSuchElementException: no such element: Unable to locate element.
Code trials:
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/Users/Documents/Coding/chromedriver')
url = "https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link"
driver.get(url)
wait = WebDriverWait(driver, 10)
elem = driver.find_element_by_xpath("//*[#id='prices-and-performance-tab']/div/div[4]/div[3]/div[1]/div[1]/div[3]/div/div/div[2]/div/table/tfoot/tr/td/a")
webdriver.ActionChains(driver).move_to_element(elem).click(elem).perform()
To click on the element wait for the page to load and element to be clickable and then click.Try entire snippet.
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path='/Users/Documents/Coding/chromedriver')
url = "https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link"
driver.get(url)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()[contains(.,'Price & Performance')]]")))
element.click
To click on the element with text as Search for more historical prices you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path = r'C:\Utility\BrowserDrivers\chromedriver.exe' )
driver.get("https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='bannerButton']"))).click()
more_historical_prices = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "Search for more historical prices")))
driver.execute_script("arguments[0].scrollIntoView(true);", more_historical_prices)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Search for more historical prices")))
Browser Snapshot:

Resources