Дженго Селен Вебдрайвер не может найти стол - PullRequest
0 голосов
/ 10 мая 2019

У меня есть код Django, в котором используется автономный драйвер селен-хром.

Я пытаюсь получить атрибуты таблицы из URL, но она не находит таблицу.Я пробовал оба find_element_by_xpath и WebDriverWait с методами EC.visibility_of_element_located.Этим двум не удалось найти таблицу.

from selenium import webdriver

def setup_driver(self):
    logging.info("Setup driver...")
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument('--disable-extensions')
    chrome_options.add_argument("--incognito")
    chrome_options.add_argument("--disable-plugins-discovery")
    chrome_options.add_argument('--user-data-dir=/tmp')
    chrome_options.add_argument('--profile-directory=Default')
    chrome_options.add_argument('--headless')

    self.driver = webdriver.Chrome(chrome_options=chrome_options)

    self.driver.implicitly_wait(15)

---- Это еще один py-файл ----

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

import pandas as pd
def __init__(self, remote=False, rental=False):

    info = {
        "url": "https://login.fmls.com/SAML/login.aspx?ReturnUrl=%2fsaml%2fSAML%2fSSOService.aspx%3fSAMLRequest%3djZLNTsMwEIRfJfKBWxLHrWhrmqCKCilSEagBDlyQ62waS7EdvE7VxydJy98BxMXyrnY934y8RKGblq86X5stvHWAPsjXKXlNaLUDyapwBxULp7tJFQo6KcOFhHkypwDsckaCZ3CorEkJiygJcsQOcoNeGN%252B3aLII6TRk88ck4RPK2Sxi08ULCda9ijLCj5u19y3yOG7sXpmo0g1G0up44IqL1d0mLor7AtxBSYgEtkcS3FonYSROSSUahEH5QSCqA3x2VojgBoUba7DT4M5v5KaEY0ro7xNP280XlRbeqeOIVUb9ca4HwtP1zD2QXbc9g6%252Bd7fZ1yi6Ebq%252BMdVAqB9KnCQmOujHIx8RT0jnDrUCF3AgNyL3kg13eJ8lbZ72VtiHZcpjmY7Du2%252F7f6%252BLDGcn%252B7WMZf1PKTtXPf5G9Aw%253D%253D%26RelayState%3dMatrix%2bSAML%2bLogin",
        "login_info": {
            "username": "your_username",
            "passwd": "your_passwd",
            "username_input_xpath": "//input[@id = 'PblcID']",
            "passwd_input_xpath": "//input[@id = 'passwordTextBox']",
        },
        "search_info": {
            "residential_url": "https://matrix.fmlsd.mlsmatrix.com/Matrix/Search/Residential",
            "rental_url": "https://matrix.fmlsd.mlsmatrix.com/Matrix/Search/ResidentialIncome",
            "status_table_xpath": "//table[@class='S_MultiStatus']"
        },
        "table_info": {
            "page_size_id": "m_ucDisplayPicker_m_ddlPageSize",
            "page_format_id": "m_ucDisplayPicker_m_ddlDisplayFormats",
            "next_button_xpath": "//span[@class='pagingLinks']/a[2]",
            "table_xpath": "//*[@id='m_pnlDisplay']/table"
        }
    }
    vars_range = range(0, 7) #
    super().__init__(info, vars_range, remote, rental)

def login_mls(self): #
    # Login
    self.driver.get(self.info['url'])
    form = self.driver.find_element_by_xpath("//form")
    user = form.find_element_by_xpath(self.info["login_info"]["username_input_xpath"])
    password = form.find_element_by_xpath(self.info["login_info"]["passwd_input_xpath"])
    user.send_keys(self.info["login_info"]["username"])
    password.send_keys(self.info["login_info"]["passwd"])
    button = form.find_element_by_xpath("//input[@id = 'loginButton']")
    button.click()

date_string ="2019-05-09"
rental = False

def search(self, date_string: str):
    # Get url
    if self.rental:
        self.driver.get(self.info["search_info"]["rental_url"])
    else:
        self.driver.get(self.info["search_info"]["residential_url"])

    inputs = self.driver.find_elements_by_xpath(self.info["search_info"]["status_table_xpath"] + "//input")
    if self.rental:
        inputs = inputs[0:10]
    else:
        inputs = inputs[0:10]
    last_idx = len(inputs) - 2

    self.driver.execute_script("window.scrollTo(0, 90);")
    for i in range(0, len(inputs), 2):
        field = inputs[i + 1]
        field.send_keys(date_string)
        checkbox = inputs[i]
        checkbox.click()
        if i == last_idx:
            field.send_keys(Keys.ENTER)

def scrape_table(self, date: pd.datetime, page: int, var: int) -> pd.DataFrame:
    try:
        table = self.driver.find_element_by_xpath(self.info["table_info"]["table_xpath"])
        table = WebDriverWait(self.driver, 15).until(EC.visibility_of_element_located((By.XPATH, self.info["table_info"]["table_xpath"])))
        table = table[0]
        html = table.get_attribute("outerHTML")
        df = pd.read_html(html)[0]
        return df
    except:
        raise MLSException("scrape_table {} p{} v{} error.".format(date, page, var))

В конце этого кода я долженчтобы получить таблицу данных панд, но на самом деле я получаю сообщение об ошибке: TimeoutException (message, screen, stacktrace) selenium.common.exceptions.TimeoutException из переменной таблицы в последней функции.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...