Селен, питон динамический стол

Я создаю робота с селеном, который получает всю информацию от агентств в Бразилии, я уже сделал щелчок перестановки между всеми штатами и округами, все, что мне нужно сделать, теперь нажмите во всех агентствах и получите информацию вроде: "telefone","endereço","HORÁRIO DE ATENDIMENTO". Как мне это сделать?? Это мой код до сих пор:

 # -*- coding: UTF-8 -*-
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
import time     
import urllib
import urllib.request
from bs4 import BeautifulSoup
import request


def make_soup(url):
    thepage = urllib.request.urlopen(url)
    soupdata = BeautifulSoup(thepage,'html.parser')
    return soupdata

chrome_path = r"D:\Users\gbenites\Desktop\Inovação\arquivos py\WebDriver\chromedriver.exe"
url = 'http://www2.correios.com.br/sistemas/agencias/'
driver = webdriver.Chrome(chrome_path)
driver.get("http://www2.correios.com.br/sistemas/agencias/")
#WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'municipioAgencia')))
driver.maximize_window()


time.sleep(3) 
estadoList = Select(driver.find_element_by_name('estadoAgencia'))
for index in range(3,len(estadoList.options)):
    select = Select(driver.find_element_by_name('estadoAgencia'))
    print("selecting Estado: ",select.options[index].text)    
    print("selecting Index: ",index)    
    select.select_by_index(index)
    time.sleep(2)
    municípioList = Select(driver.find_element_by_name('municipioAgencia'))
    for index_b in range(1,len(municípioList.options)):
        select_b = Select(driver.find_element_by_name('municipioAgencia'))
        print("...selecting Municipio",select_b.options[index_b].text.replace("'",""))
        print("...selecting Municipio Index: ",index_b)
        select_b.select_by_index(index_b)
        time.sleep(3)
        get_info_list =  WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//table[@class = 'dadosAgencia']//td")))
        info_list = driver.find_elements_by_xpath("//table[@class = 'dadosAgencia']//span")
        print(range(len(info_list)))
        for record in range(0,len(info_list)):
            print(record.get_attribute('innerText'))

И это мой вывод:

selecting Estado:  AMAPÁ
selecting Index:  3
...selecting Municipio AMAPA
...selecting Municipio Index:  1
range(0, 27)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

2 ответа

Попробуйте этот xPath:

//table[@class = 'dadosAgencia']//tr

Это было бы так:

elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located(
            (By.XPATH, "//table[@class = 'dadosAgencia']//tr")))

это дает вам список всех расположенных элементов. Для печати текста каждого элемента вы можете использовать это:

for element in elements:
    print(element.text)

Примечание: вы должны добавить несколько импортов:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

Попробуйте этот XPath для таблицы:

//table[@class = 'dadosAgencia']

Сначала найдите элементы таблицы:

table_elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located(
        (By.XPATH, "//table[@class = 'dadosAgencia']")))

Итерация по таблице:

for table_element in table_elements:
    for row in table_element.find_elements_by_xpath(".//tr"):
        print(row.text)
Другие вопросы по тегам