简体   繁体   中英

Python Selenium Can not find ID inside Div

在此处输入图片说明 So I am trying to automate my daily thing to login to my ADP website but it seems whatever function I pick from selenium is does not find the ID that I am looking for which is "id="login-form_username".

Below is the part of the Selenim Python code.

user_textbox=driver.find_element_by_id('login-form_username')
user_textbox.send_keys(user)
user_textbox.click()

user_confirm=driver.find_element_by_id("verifUseridBtn")
user_pass_textbox=driver.find_element_by_id("login-form_password")

user_signin=driver.find_element_by_id("signBtn")
user_signin.send_keys(Keys.RETURN)

Also Attached screenshot of the site and error from my IDE. Im not sure if its because for security reasons that the Website I am trying to access wont allow this kind of thing.

Thanks Joel在此处输入图片说明

Since you didn't share the page link and not the error you seeing we can only guess what is wrong with your code.
So it can be:

  1. You are missing a wait / delay before accessing that element.
    In this case something like this should resolve your issue:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path='chromedriver.exe')

wait = WebDriverWait(driver, 20)
driver.get("https://your_site_url/")
user_textbox = wait.until(EC.visibility_of_element_located((By.ID, "login-form_username")))
user_textbox.send_keys(user)
user_textbox.click()

Alternatively you can simply add a delay before locating the elements, something like this:

time.sleep(5)
user_textbox=driver.find_element_by_id('login-form_username')
user_textbox.send_keys(user)
user_textbox.click()

time.sleep(5)
user_confirm=driver.find_element_by_id("verifUseridBtn")
user_pass_textbox=driver.find_element_by_id("login-form_password")

time.sleep(5)
user_signin=driver.find_element_by_id("signBtn")
user_signin.send_keys(Keys.RETURN)

but this is not the best approach.
2) Maybe there is an iframe there.
In this case you have to switch to that iframe.
3) Maybe you didn't set web driver to large enough size - if so please do that.
4) Possibly you have to scroll that input element into the view?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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