Python 3 使用 selenium 进行网页抓取:ui-对话框切换时出现问题

Python 3 web scraping with selenium : ui-dialog trouble with switching

我是一名学生,刚接触 Python。我想从网站下载 pdf 文件(这些是来自不同组织的财务报告),但在此之前我必须完成一些步骤。 这是我正在处理的网站:http://sprawozdaniaopp.mpips.gov.pl/ 这里有很多组织,所以我认为下载带有脚本的pdf会很好。首先,我的脚本单击“搜索”按钮(没有任何条件 - 以查找所有)-> 作为 links 加载的整个列表的效果。当我单击 link -> 较小的 window 出现在同一站点上(此 window 仅指我单击的组织)。而且 - 这就是问题所在 - 我的脚本无法切换到 window。我在互联网上搜索并找到 driver.switch_to.window 或 driver.switch_to.frame 函数,但它没有用,或者我没有正确使用它。恐怕这不是任何框架,而是 ui-dialog(?)。当我在这个 window 上单击右键并检查这个 window 时,我发现了类似的东西:

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

不知道如何告诉我的脚本切换到这种对话框 window (?) 以启用它仅搜索 2016 年的 link "Sprawozdanie merytoryczne"。

这个站点的奇怪之处在于,当我检查 link 时,有例如:http://sprawozdaniaopp.mpips.gov.pl/Search/Details/0000000168 只有单击左键才能打开它。当我尝试在新选项卡中打开它时,这是不可能的(为什么?)。效果如下: “‘/’应用程序中的服务器错误。 无法找到该资源。 说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下 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()

我附上一些屏幕来说明我的问题。我将非常感谢任何建议或我应该寻找的一些关键词(也许情况很明显,我作为新手不明白)。您好!

partial list of organizations wanted pdf document which opens in new tab after clicking on yellow link

在网站 http://sprawozdaniaopp.mpips.gov.pl/ 上单击 Search 按钮并单击第一个 link 我们需要等待 Modal Box 打开然后我们必须点击 Sprawozdanie merytoryczne link。这是您自己的代码,经过简单调整如下:

elem1 = driver.find_element_by_css_selector("#form1 > div > div.grid > table > tbody > tr:nth-child(1) > td:nth-child(3) > a")
elem1.click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,".ui-dialog.ui-widget.ui-widget-content.ui-corner-all")))
driver.find_element_by_link_text("Sprawozdanie merytoryczne").click()