首先,我从不同的下拉菜单中选择选项,然后单击show按钮,然后尝试从html表中提取值时,没有找到任何元素。你知道为什么吗
driver = webdriver.Chrome('/Users/Administrador/Documents/chromedriver')
main_url = 'https://www.justiciacordoba.gob.ar/Estatico/JEL/Escrutinios/ReportesEleccion20190512/default.html'
driver.get(main_url)
# This works perfectly
driver.switch_to.frame("topFrame")
dropdown_secciones = driver.find_element_by_xpath('./html/body/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/select')
select_box_secciones = Select(dropdown_secciones)
select_box_secciones.select_by_value("1|308")
dropdown_circuitos = driver.find_element_by_xpath('//*[@id="cmbCircuitos"]')
select_box_circuitos = Select(dropdown_circuitos)
select_box_circuitos.select_by_index(1)
mostrar_click = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table/tbody/tr[3]/td/div/input[1]')
mostrar_click.click()
driver.switch_to.default_content()
driver.switch_to.frame('mainFrame')
# This doesn´t work. No element is found.
for r in range(8,35): #from row 8 up to row 35
for c in range(3,7): #starting in column 3 up to column 7
value = driver.find_element_by_xpath('/html/body/table/tbody/tr["+str(r)+"]/td["+str(c)+"]').text
print(value)
尝试从表中提取值时未找到任何元素
我看了一下表,问题是有些行可能有空列(尝试获取tr[9],您就会明白我的意思
另外,对于包含3列值的行,您可能已经知道,有些行的td元素比其他行少
因此,您可以保留8-35范围,但在此范围内,您可以使用以下xpath获取所有实际包含文本的列(使用find_元素获取列表)
xpath应该始终为您提供3个元素,每个元素对应于中包含文本的列。如前所述,某些行将为空,但您可以轻松处理,因为列结果的计数为0
下面是一段您可能感兴趣的代码
我认为,与xpath操作相比,使用BeautifulSoup进行html解析会更好
下面代码的思想是:
这种方法的优点:
换句话说,这完全是相对的
上述脚本的输出为:
相关问题 更多 >
编程相关推荐