Webscraping селена Python возвращает не могу найти элемент - PullRequest
2 голосов
/ 15 июня 2019

У меня есть следующая веб-страница, на которой мне нужно войти в систему, используя имя пользователя и пароль.

<div class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"><label class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-shrink MuiInputLabel-outlined Mui-required Mui-required" data-shrink="true">Username<span class="MuiFormLabel-asterisk MuiInputLabel-asterisk"> *</span></label><div class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl"><fieldset aria-hidden="true" class="jss298 MuiOutlinedInput-notchedOutline" style="padding-left: 8px;"><legend class="jss299" style="width: 70.25px;"><span>​</span></legend></fieldset><input aria-invalid="false" autocomplete="email username" class="MuiInputBase-input MuiOutlinedInput-input" required="" type="text" autocapitalize="none" value=""></div></div>"

Мой код указан ниже

uname = driver.find_element_by_class_name('MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth')

Однако возвращается следующая ошибка:

NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"}
  (Session info: chrome=75.0.3770.9

пожалуйста, вы можете мне помочь?

1 Ответ

0 голосов
/ 16 июня 2019

Чтобы отправить последовательность символов в нужное поле, вы должны ввести WebDriverWait для element_to_be_clickable(), и вы можете использовать любое из следующих решений:

  • Использование CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.MuiInputBase-root.MuiOutlinedInput-root.MuiInputBase-fullWidth.MuiInputBase-formControl input.MuiInputBase-input.MuiOutlinedInput-input[autocomplete='email username']"))).send_keys("bluemountains1979")
    
  • Использование XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl']//input[@class='MuiInputBase-input MuiOutlinedInput-input' and @autocomplete='email username']"))).send_keys("bluemountains1979")
    
  • Примечание: Вы должны добавить следующие импорты:

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