Reputation: 15
I'm trying to use Selenium to parse a website of sensor data (take a look: https://map.purpleair.org/sensorlist). 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("https://map.purpleair.org/sensorlist")
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()
Upvotes: 0
Views: 1004
Reputation: 821
When you execute the line
table_datas.find_element(By.XPATH, '//button[text()="Download Primary"]').click()
You will find the first button on the page with "Download Primary" on it.
You can however find the row with Paso Robles and from there find the appropriate button:
List<WebElement> columnsOfRow =
driver.find_elements_by_css_selector("div[id='thelist'] tr td:nth-child(2):contains('Paso Robles')");
When you want to find all the buttons of the rows with Paso Robles in it, you can use XPath. It will become something like:
List<WebElement> allButtonsWithPasoRoblesInRow =
driver.find_elements_by_xpath("//tr[contains(td[2], 'Paso Robles')]/td[5]/button[text()='Download Primary']");
Upvotes: 1