私は学生で、Python は初めてです。Web サイトから PDF ファイル (さまざまな組織からの財務報告書) をダウンロードしたいのですが、その前にいくつかの手順を実行する必要があります。これが私が扱っているウェブサイトです: http://sprawozdaniaopp.mpips.gov.pl/ こちらは団体が多いので、スクリプト付きのpdfをダウンロードすると良いと思いました。まず、私のスクリプトは [検索] ボタンをクリックします (条件なしで - すべてを検索するため) -> リンクのリスト全体が読み込まれます。リンクをクリックすると、同じサイトに小さなウィンドウが表示されます (このウィンドウは、クリックした組織のみを参照します)。そして - ここに問題があります - 私のスクリプトはそのウィンドウに切り替えることができません。インターネットを検索していて、driver.switch_to.window または driver.switch_to.frame 関数を見つけましたが、機能しないか、正しく使用していませんでした。残念ながら、これはフレームではなく ui-dialog(?) です。このウィンドウの右ボタンをクリックしてこのウィンドウを調べると、次のようなものが見つかりました。
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-2" style="display: block; z-index: 1002; outline: 0px; height: auto; width: 600px; top: 234.5px; left: 328px;"><div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title" id="ui-dialog-title-2">Szczegółowe informacje o organizacji</span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div><div style="width: auto; min-height: 0px; height: 401.896px;" class="ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0"> (...)
スクリプトにこの種のダイアログ ウィンドウ (?) に切り替えて、2016 年のみリンク "Sprawozdanie merytoryczne" を検索できるようにする方法がわかりません。
このサイトの奇妙な点は、リンクを確認すると、たとえばhttp://sprawozdaniaopp.mpips.gov.pl/Search/Details/0000000168があることです。左ボタンをクリックするだけで開くことができました。新しいタブで開こうとするとできません (なぜですか?)。結果は次のとおりです。「「/」アプリケーションでサーバー エラーが発生しました。リソースが見つかりません。説明: HTTP 404。探しているリソース (またはその依存関係の 1 つ) が削除されたか、名前が変更された可能性があります。は一時的に利用できません。次の URL を確認し、スペルが正しいことを確認してください。」
Pythonでの私のスクリプトは次のとおりです。
import urllib
import urllib.request
import requests
import re
url = "http://sprawozdaniaopp.mpips.gov.pl/Search/Print/13313?reporttypeId=13"
r = requests.get(url)
#with open(r'C:\Users\username\Desktop\financialreport1.pdf', 'wb') as f:
# f.write(r.content)
from selenium import webdriver
chrome_path= r"C:\Users\username\AppData\Local\Programs\Python\Python35-32\Scripts\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("http://sprawozdaniaopp.mpips.gov.pl/")
#Button Search called here in polish "Znajdź"
elem = driver.find_element_by_xpath("//*[@id='btnsearch']/span")
elem.click()
#testing if I'm able to find links on this website
#elems = driver.find_elements_by_xpath("//a[@href]")
#for elem in elems:
#print (elem.get_attribute("href"))
#Clicking on first link ( in future I wanted to do it in loop for every link
#elem1 = driver.find_element_by_xpath("//*[@id='form1']/div/div[4]/table/tbody/tr[1]/td[3]/a")
elem1 = driver.find_element_by_css_selector("#form1 > div > div.grid > table > tbody > tr:nth-child(1) > td:nth-child(3) > a")
elem1.click()
#doesn't work
#driver.switch_to.window("#form1 > div > div.grid > table > tbody > tr:nth-child(1) > td:nth-child(3) > a")
#below doesn't work because I can't switch to window where elem2 is placed
elem2 = driver.find_element_by_css_selector("body > div.ui-dialog.ui-widget.ui-widget-content.ui-corner-all > div.ui-dialog-content.ui-widget-content > table:nth-child(4) > tbody > tr:nth-child(7) > td:nth-child(1) > a")
elem2.click()
私の問題を説明するためにいくつかの画面を添付します。アドバイスや探すべきキーワードがあれば、非常に感謝しています (おそらく、そのケースは明らかであり、初心者としては理解できません)。ご挨拶!