Selenium 无法按名称或 ID 找到元素 (Python)

Selenium cannot find element by name or id (Python)

我正在尝试使用 selenium 自动登录网站,但我收到“没有这样的元素消息”错误。这是我的代码,其中包含网站的 link:

from selenium import webdriver
import time
import datetime

driver = webdriver.Chrome("C:\Users\Family\Downloads\chromedriver_win32\chromedriver.exe")
driver.get("https://login.microsoftonline.com/c4d72b4d-8155-4a90-9155-7705148c41ca/saml2?SAMLRequest=jdE9a8MwEAbgvdD%2fYLRbkh3ZVoQdCO0SSJek7dClnJVzYrClVCeX%2fvw6DaEdu90HLzzc1espntwOPyakmGweG0YwDuHav3eqqFSHOZRQKZAZdJDpDjToKisXiCx5xUC9dw3LuWTJhmjCjaMILs4jmWepVKnMnrPCyNIscq601pVUbyxZE2GIc%2fbBO5pGDHsMn73Fl922YacYz2SEiAdqOQ4IwfXu6F2E0HtuQRzyQQxnAbNeDP7YO3Fxby8Vn3cs%2bRoHRw2bgjMeqCfjYEQy0Zr9%2bmlrZq45Bx%2b99QNb3d8lSf2DD%2f8Jwo3OVjdokdlSVrZKUS10quTSphrLIi00drrVebksWh7RzYch3ob%2beIp0Bovc%2bvGXXosrYgbV4u9nVt8%3d&RelayState=%2fd2l%2fhome&sso_reload=true")

login_button = driver.find_element_by_id("i0116")
login_button.send_keys("sajjad.jessa@student.tdsb.on.ca")

这是我尝试使用我的代码访问的元素:

<input type="email" name="loginfmt" id="i0116" maxlength="113" lang="en" class="form-control ltr_override input ext-input text-box ext-text-box" aria-required="true" data-bind="
                    externalCss: {
                        'input': true,
                        'text-box': true,
                        'has-error': usernameTextbox.error },
                    ariaLabel: tenantBranding.UserIdLabel || str['CT_PWD_STR_Username_AriaLabel'],
                    ariaDescribedBy: 'loginHeader' + (pageDescription &amp;&amp; !svr.fHideLoginDesc ? ' loginDescription' : ''),
                    textInput: usernameTextbox.value,
                    hasFocusEx: usernameTextbox.focused,
                    placeholder: $placeholderText" aria-label="Enter your TDSB email address here, then click Next" aria-describedby="loginHeader" placeholder="Enter your TDSB email address here, then click Next">
                    

根据其他答案,我了解到您必须使用 driver.find_element_by_css_selector() 和 driver.switch_to.frame(),但是如果您查看网站的完整超文本,要进入的第一个框架是没有任何属性的“div”标签。然而,它是唯一的“div”标签和两个“script”标签。我需要正确的代码进入框架,或其他自动登录的方法。

现在大多数大型网站都使用动态布局。您应该使用静态 css 选择器查找 parents,然后执行类似的操作:

.parent_css_selector > div:nth-child(1) > div:nth-child(3) > img > input

令牌“>”应该只查找直接 childs,令牌“:nth-child”断言 parent 中的 child 数字。

示例:

<div class="parent_css_selector">
    <div id="random_id_12312313">
        <div id="unneccesary_123123"></div>
        <div id="random_id_341234">
            <form id="random_id_345545">
                <span></span>
                <span></span>
                <span></span>
                <span></span>
                <span>
                    <input id="neccesary_13843942"></input>
                </span>
            </form>
        </div>
    </div>
</div>

您可以使用此选择器访问输入:

.parent_css_selector > div > div:nth-child(2) > form > span:nth-child(5) > input

看来是同步问题。 诱导WebDriverWait()等待element_to_be_clickable()

login_button =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,"i0116")))
login_button.send_keys("sajjad.jessa@student.tdsb.on.ca")

您需要导入以下库。

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

login_button =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.NAME,"loginfmt")))