0

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()

1 Answer 1

1

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']");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I python-ized it a tad, but it's essentially the same thing and works. Notably if I wanted to download a series of buttons that satisfy some criteria (e.g. had the word "NASA" in it), I had to put in a small time delay between clicks but it works. allButtons = driver.find_elements_by_xpath("//tr[contains(td[2], 'NASA')]/td[5]/button[text()='Download Primary']") for buttons in allButtons: buttons.click() time.sleep(3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.