简体   繁体   English

Selenium Python3 - 无法按名称找到元素

[英]Selenium Python3 - Unable to find element by its name

The code of the HTML element is as follows: HTML 元素的代码如下:

<input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="none" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value="">

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="username"]

I get the error above when I execute this code:执行此代码时出现上述错误:

from selenium.webdriver import Firefox

class Efrit:

    #Initializing the bot
    def __init__(self, username, password):
        self.username = username    # it could also be e-mail or phone number
        self.password = password

#Credentials to log into Instagram:
    def log(self):
        driver = Firefox('/usr/local/bin')
        driver.get("https://www.instagram.com/")
        username_location = driver.find_element_by_name('username')
        password_location = driver.find_element_by_name('password')

bot = Efrit('test, 'test')
bot.log()

You can use explicit wait:您可以使用显式等待:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://www.instagram.com/")
wait = WebDriverWait(driver, 20)
            wait.until(EC.visibility_of_element_located((SelectBy.CSS, "/input[name=username]")))
        username_location = driver.find_element_by_css_selector('input[name=username]')
        password_location = driver.find_element_by_css_selector('input[name=password]')

To wait for login button use element_to_be_clickable等待登录按钮使用element_to_be_clickable

wait.until(EC.element_to_be_clickable(
                (SelectBy.CSS_SELECTOR, ".sqdOP.L3NKy.y3zKF")))
login = driver.find_element_by_css_selector(".sqdOP.L3NKy.y3zKF")
login.click()

You can also set the global implicit wait.您还可以设置全局隐式等待。 https://selenium-python.readthedocs.io/waits.html (refer to P.5.2 for instructions). https://selenium-python.readthedocs.io/waits.html (有关说明,请参阅 P.5.2)。

As said in the comments by JaSON, the login form is not in page source and it takes time to render.正如 JaSON 在评论中所说,登录表单不在页面源中,渲染需要时间。 You have to use wait你必须使用等待

Explicit Wait显式等待

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

    class Efrit:
        #Initializing the bot
        def __init__(self, username, password):
            self.username = username    # it could also be e-mail or phone number
            self.password = password
    
    #Credentials to log into Instagram:
        def log(self):
            driver = Firefox('/usr/local/bin')
            driver.get("https://www.instagram.com/")
            delay = 6 # seconds
            try:
                username_location = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.NAME, 'username')))
                password_location = driver.find_element_by_name('password')
                print(username_location, password_location)
            except TimeoutException:
                print("Loading took too much time!")
            driver.quit()
    
    bot = Efrit('test', 'test')
    bot.log()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM