2017-09-28 10 views
0

私はセンサーデータのWebサイトを解析するためにSeleniumを使用しようとしています(見てください:https://map.purpleair.org/sensorlist)。このWebページにはセンサーのテーブルがあり、情報は最初の4つの列に格納され、CSVファイルをダウンロードするボタンは5番目の列に格納されます。Seleniumを使用してテーブルに埋め込まれた選択ボタン(Python)

行が特定の条件を満たす場合(たとえば、特定の場所にある特定のセンサーなど)、そのボタンを「クリック」するだけです。私は基本的にSelenium webdriversを誤解しているかもしれませんが、以下のコードを実行しようとすると、私は見つけたいと思っていたものではなく "Download Primary"ボタン(Sensor#1)の最初のインスタンスだけを "クリック" 。これを行うより良い方法はありますか?あなたがラインに

table_datas.find_element(By.XPATH, '//button[text()="Download Primary"]').click() 

を実行すると

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

答えて

0

あなたはそれの「プライマリのダウンロード」のページの最初のボタンがあります。

ただしパソロブレスと行を見つけて、そこから適切なボタンを見つけることができます:あなたはそれにパソロブレスと行のすべてのボタンを見つけたいとき

List<WebElement> columnsOfRow = 
driver.find_elements_by_css_selector("div[id='thelist'] tr td:nth-child(2):contains('Paso Robles')"); 

、あなたは、XPathを使用することができます。

List<WebElement> allButtonsWithPasoRoblesInRow = 
driver.find_elements_by_xpath("//tr[contains(td[2], 'Paso Robles')]/td[5]/button[text()='Download Primary']"); 
+0

ありがとうございます!私はそれを少しだけpython化しましたが、それは本質的に同じことであり、動作します。特に、いくつかの条件を満たす一連のボタン(たとえば「NASA​​」という単語が含まれています)をダウンロードしたい場合は、クリック間に少しの時間を置く必要がありましたが、機能します。 allButtonsのボタンのための :すべてのボタンのためのすべてのボタン:driver.find_elements_by_xpath( "// tr [(td [2]、 'NASA')を含む]/td [5] buttons.click() time.sleep(3) – Ross

関連する問題