jeudi 28 septembre 2017

Using Selenium to "click" on select buttons imbedded in a table (with Python)

I'm trying to use Selenium to parse a website of sensor data (take a look: http://ift.tt/2xIRKwh). This web page has a table of sensors, where the information are stored in the first four columns while buttons to download a CSV file are in the fifth column.

The thing is, I want to only "click" on that button if the row satisfies certain conditions (e.g. it's a specific sensor located in a specific location). I may be fundamentally misunderstanding Selenium webdrivers, but when I try to execute the below code, it ends up "clicking" only the very first instance of the "Download Primary" button (Sensor #1) instead of the one I was hoping to find. Is there a better way of doing this?

driver = webdriver.PhantomJS(executable_path='./phantomjs')
driver.get("http://ift.tt/2xIRKwh")
assert "PurpleAir" in driver.title
time.sleep(3)

# Select Dates here
startdate = driver.find_element_by_id("startdatepicker")
enddate = driver.find_element_by_id("enddatepicker")
startdate.send_keys('09/25/17', Keys.ENTER)
enddate.send_keys('09/27/17', Keys.ENTER)

# Parse table and find table elements that fit a certain criteria
for table_rows in driver.find_elements(By.TAG_NAME, "tr"):
    table_datas = table_rows.find_elements(By.TAG_NAME, "td")
    if 'Paso Robles' in table_datas[1].text:
        print(table_datas[0].text, table_datas[1].text)
        table_datas.find_element(By.XPATH, '//button[text()="Download Primary"]').click()




Aucun commentaire:

Enregistrer un commentaire