如何使用Selenium从页面中删除所有数据?

时间:2017-07-23 08:51:41

标签: python selenium web-scraping

我无法点击日期按钮。

from selenium import webdriver
browser = webdriver.Chrome()
url = "http://rate.am/en/armenian-dram-exchange-rates/banks/non-cash"
browser.get(url)
from selenium import *
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from time import*

导入所有必要的工具后,我尝试编写将转到页面的代码,点击历史数据并按日期查看,但点击日期按钮时遇到问题

browser = webdriver.Chrome()
url = "http://rate.am/en/armenian-dram-exchange-rates/banks/non-cash"
browser.get(url)
all_data=browser.find_element_by_xpath('//*[@id="aspnetForm"]/div[3]/div[2]/div[1]/div[4]/div[3]/table/tbody/tr/td[1]/a')
all_data.click()
years=browser.find_element_by_xpath('//*[@id="calBorder"]/span/table/tbody/tr[1]/td/table/tbody/tr/td[2]/select')
for i in range(len(years.find_elements_by_tag_name('option'))):    
    for j in range(12):
        for z in range(31):
            time=browser.find_element_by_xpath('//*[@id="ctl00_Content_RB_dtpick1_dpHours"]')
            for x in range(len(time.find_elements_by_tag_name('option'))):
                all_data=browser.find_element_by_xpath('//*[@id="aspnetForm"]/div[3]/div[2]/div[1]/div[4]/div[3]/table/tbody/tr/td[1]/a')
                all_data.click()
                sleep(0.3)                
                yearss = browser.find_element_by_xpath('//*[@id="calBorder"]/span/table/tbody/tr[1]/td/table/tbody/tr/td[2]/select')
                sy = Select(yearss)
                sy.select_by_index(i)
                sleep(0.3)
                months = browser.find_element_by_xpath('//*[@id="calBorder"]/span/table/tbody/tr[1]/td/table/tbody/tr/td[1]/select')
                sm = Select(months)
                sm.select_by_index(j)
                sleep(0.3)
                days=browser.find_element_by_xpath('//*[@id="calBorder"]/span/table/tbody/tr[3]/td/table/tbody/tr[2]/td[6]')
                if 'bgcolor="white"'==None in days:
                    days.click()
                    sleep(0.3)
                times=browser.find_element_by_xpath('//*[@id="ctl00_Content_RB_dtpick1_dpHours"]')
                ti=Select(times)
                ti.select_by_index(x)
                sleep(0.3)
                show=browser.find_element_by_xpath('//*[@id="ctl00_Content_RB_dtpick1_btnOk"]')
                show.click()
                sleep(0.3)

1 个答案:

答案 0 :(得分:0)

我没有处理这一年,但模式是一样的。就选择日期而言,我创建了一个xpath,它将识别所有td元素,然后选择那些文本等于给定日期的元素,即23,这对我来说只是一个随机选择。 / p>

>>> from selenium import webdriver
>>> browser = webdriver.Chrome()
>>> url = 'http://rate.am/en/armenian-dram-exchange-rates/banks/non-cash'
>>> browser.get(url)
>>> browser.find_element_by_xpath('.//a[@class="date-picker"]').click()
>>> browser.find_element_by_xpath('.//select[@name="MonthSelector"]').click()
>>> browser.find_element_by_xpath('.//select[@name="MonthSelector"]/option[1]').click()
>>> browser.find_elements_by_xpath('.//td[@class="calTD"]')
>>> browser.find_elements_by_xpath('.//td[@class="calTD" and text()="23"]')[0].click()
>>> browser.find_element_by_xpath('.//input[@id="ctl00_Content_RB_dtpick1_btnOk"]').click()