简体   繁体   English

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

[英]Selenium cannot find element by name or id (Python)

I am trying to automate logging into a website using selenium, but I am getting "no such element message" error.我正在尝试使用 selenium 自动登录网站,但出现“没有此类元素消息”错误。 Here is my code, with the link to the website included:这是我的代码,其中包含指向该网站的链接:

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")

And here is the element I am trying to access with my code:这是我试图用我的代码访问的元素:

 <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">

From other answers I understand that you have to use driver.find_element_by_css_selector() and driver.switch_to.frame(), but if you look at the full hypertext of the website, the first frame to go into is a "div" tag without any attributes.从其他答案我了解到您必须使用 driver.find_element_by_css_selector() 和 driver.switch_to.frame(),但是如果您查看网站的完整超文本,到 go 的第一帧是一个“div”标签,没有任何属性。 It is however the only "div" tag alongside two "script" tags.然而,它是两个“脚本”标签旁边的唯一“div”标签。 I need the correct code to go into the frame, or another method to automate logging in.我需要将 go 的正确代码放入框架中,或者使用其他方法自动登录。

The most of the large websites now uses dynamic layout.现在大多数大型网站都使用动态布局。 You should look for the parents with static css selector, and then do something like that:您应该使用 static css 选择器查找父母,然后执行以下操作:

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

Token '>' is supposed to look only for direct childs, token ':nth-child' asserts to the child number in parent.标记 '>' 应该只查找直接子代,标记 ':nth-child' 断言父代中的子代号。

Example:例子:

<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>

You can use this selector to access the input:您可以使用此选择器访问输入:

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

It seems synchronization Issue.似乎是同步问题。 Induce WebDriverWait() and wait for element_to_be_clickable()诱导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")

You need to import following libraries.您需要导入以下库。

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")))

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

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