0
我仍然刮本网站http://bombayhighcourt.nic.in/party_query.php战斗。硒刮刀 - 循环逻辑
我想刮它通过使用下拉值的所有可能组合的用户选择年。
所以这是我的代码的重要棋子:
class Scraper(webdriver.Chrome):
def __init__(self, url):
self.url = url
self.mydriver = webdriver.Chrome()
self.mydriver.get(self.url)
def chooseDropdownOption(self, xpath, option):
dropdown = self.mydriver.find_element_by_xpath(xpath)
Select(dropdown).select_by_value(option)
def getDropdownOptions(self, xpath):
dropdown = self.mydriver.find_element_by_xpath(xpath)
return UI.Select(dropdown).options
(...)
#now I extract all options from each dropdown:
bench_options = s.getDropdownOptions("/html/body/form/table[2]/tbody/tr/td[2]/select")
jurisd_options = s.getDropdownOptions("/html/body/form/table[2]/tbody/tr/td[4]/select")
petition_options = s.getDropdownOptions("/html/body/form/table[4]/tbody/tr/td[2]/select")
#and now I am looping through each list:
for bench_option in bench_options:
#LOOP 1
current_bench_option = bench_option.get_attribute('value')
for jurisd_option in jurisd_options:
#LOOP 2
current_jurisd_option = jurisd_option.get_attribute("value")
for petition_option in petition_options:
#LOOP 3
current_petition_option = petition_option.get_attribute("value")
for year in range(year_start, year_final+1):
#LOOP 4
s.chooseDropdownOption("/html/body/form/table[2]/tbody/tr/td[2]/select", current_bench_option)
s.chooseDropdownOption("/html/body/form/table[2]/tbody/tr/td[4]/select", current_jurisd_option)
s.inputText("/html/body/form/table[3]/tbody/tr/td[2]/input[2]", name)
s.chooseDropdownOption("/html/body/form/table[4]/tbody/tr/td[2]/select", current_petition_option)
s.chooseDropdownOption("/html/body/form/table[4]/tbody/tr/td[4]/select", str(year))
s.clickButton("/html/body/form/table[5]/tbody/tr[1]/td/input[1]")
#DO SCRAPING PART
#GO BACK to THE SEARCH PAGE
我不知道是否有做这种任务的更清洁的方式,但在这里,这不是我的问题。我的这个程序的理解如下:
- LOOP 1 - 从列表中选择(印度孟买)
- LOOP 2第一个选项 - 从列表中选择第一个选项(土木)
- LOOP 3 - 选择第一从列表(呈请)
- LOOP 4选项 - 重复它为每个在范围 的岁月。当岁月的范围耗尽我认为接下来的点是:
- LOOP 3 - 选择从第二个选项列表(Respondend)
- LOOP 4 - 所有年份
但范围耗尽它正确地返回到搜索页面,但我得到这个错误信息的岁月之后再重申一遍:
---------------------------------------------------------------------------
StaleElementReferenceException Traceback (most recent call last)
<ipython-input-34-09f2da0eea05> in <module>()
27
28 for petition_option in petition_options:
---> 29 current_petition_option = petition_option.get_attribute("value")
30 print(current_petition_option)
31
(...)
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64)
东西是失败的我理解,但我不能找到什么...
你应该注意的是,当你选择'“板凳”'或'“管辖权”的值'你做出新的'HTTP'请求到服务器,让你获得新的一页。这意味着,网络元素定义之前选择成为独立的,以'DOM',所以你不能处理他们了,但需要重新定义它们。希望这可以帮助你了解问题的原因:) – Andersson
好点!谢谢 – pawelty