Reputation: 39
I am trying to find the element of a button (either the "Download" or "Save selected series codes" buttons) of a webpage but I am unable to do so with whichever method I try. The webpage does not have an iFrame, the explicit wait method does not work, and in fact, even worse, when I iterate through all classes of the webpage, the element doesn't show up, while some elements that do not appear in the 'inspect elements' of chrome, do.
x = browser.find_elements_by_xpath('//*[@class]')
for y in x:
print(y.get_attribute('class'))
Furthermore, another unexpected issue is the following:
The <div class="contentsInner">
element, which has the following three children,
<div class="frameDecorationPlane clearfix">
<div class="abstractResultArea">
<div class="contentsBorder">
Can be located, either with xpath or css selector, but except for the first child element, the remaining two raise a no such element error.
I have included a screenshot of the webpage that I am referring to below, for reference.
Lastly, I have included below the code that I have used to get to the website's page that raises the error. In order to replicate the error, you can use the following link, and just select any random item and any output year range and just click the 'search' button. You don't necessarily have to select specifically the data that I have selected for export.
# We open a browser window.
browser = webdriver.Chrome(executable_path='/Users/George/chromedriver_80', chrome_options=chromeOptions)
# We load the website's query:
browser.get('http://www.stat-search.boj.or.jp/ssi/cgi-bin/famecgi2?cgi=$nme_a000_en&lstSelection=PR01')
# Finds and clicks the Corporate Goods Price Index (2015 base) element.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.selectMenuItem > table > tbody > tr.selectedMenu > td.menuNameCell')
element.click()
# Finds and clicks the next button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.resultCountPanel.clearfix > input')
element.click()
# Finds and clicks the Producer Price Index element.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.selectMenuItem > table > tbody > tr.selectedMenu > td.menuNameCell')
element.click()
# Finds and clicks the next button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.resultCountPanel.clearfix > input')
element.click()
# Finds and clicks the All commodities element.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.selectMenuItem > table > tbody > tr.selectedMenu > td.menuNameCell')
element.click()
# Finds and clicks the next button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.resultCountPanel.clearfix > input')
element.click()
# Finds and ticks the PR01'PRCG15_2200000000 PPI indicator element.
element = browser.find_element_by_css_selector('#menuSearchDataCodeList > tbody > tr:nth-child(1) > td > label')
element.click()
# Finds and clicks the add to search condition button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.leafMenu > div:nth-child(6) > a')
element.click()
# Types into the 'From YYYY' output range.
browser.find_element_by_css_selector('#fromYear').send_keys('1960') # The start year is '1960'.
# Finds and clicks the search button.
element = browser.find_element_by_css_selector('#resultArea > div.abstractionMenuArea.clearfix > div:nth-child(1) > a:nth-child(1)')
element.click()
After that, this is the line that raises the error:
element = browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/table/tbody/tr[2]/td[4]/a')
Upvotes: 0
Views: 166
Reputation: 4177
Please find below working solution. You need to switch to tab and then click on that element to download your report.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import win32com.client as comclt
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
browser = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
browser.get('http://www.stat-search.boj.or.jp/ssi/cgi-bin/famecgi2?cgi=$nme_a000_en&lstSelection=PR01')
# Finds and clicks the Corporate Goods Price Index (2015 base) element.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.selectMenuItem > table > tbody > tr.selectedMenu > td.menuNameCell')
element.click()
# Finds and clicks the next button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.resultCountPanel.clearfix > input')
element.click()
# Finds and clicks the Producer Price Index element.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.selectMenuItem > table > tbody > tr.selectedMenu > td.menuNameCell')
element.click()
# Finds and clicks the next button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.resultCountPanel.clearfix > input')
element.click()
# Finds and clicks the All commodities element.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.selectMenuItem > table > tbody > tr.selectedMenu > td.menuNameCell')
element.click()
# Finds and clicks the next button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.nodeMenu > div.resultCountPanel.clearfix > input')
element.click()
# Finds and ticks the PR01'PRCG15_2200000000 PPI indicator element.
element = browser.find_element_by_css_selector('#menuSearchDataCodeList > tbody > tr:nth-child(1) > td > label')
element.click()
# Finds and clicks the add to search condition button.
element = browser.find_element_by_css_selector('body > div.contents > div > ul.tabContent > li:nth-child(1) > div.searchCondition > div.leafMenu > div:nth-child(6) > a')
element.click()
# Types into the 'From YYYY' output range.
browser.find_element_by_css_selector('#fromYear').send_keys('1960') # The start year is '1960'.
# Finds and clicks the search button.
element = browser.find_element_by_css_selector('#resultArea > div.abstractionMenuArea.clearfix > div:nth-child(1) > a:nth-child(1)')
element.click()
browser.switch_to.window(browser.window_handles[-1])
print browser.title
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//a[@class='littlelargeButton largeButton-download']"))).click();
Output: Kindly find below output once you click onto the download link
Upvotes: 1