简体   繁体   中英

Message: no such element: Unable to locate element: {“method”:“css selector”,“selector”:“[id=”loginUsername“]”}

I am trying to learn Web Scraping with Python using Selenium. I am testing Reddit.com. I am stuck here. When I run the script it stops here on the login page and gives the following error: (selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="loginUsername"]"}) Please note that the same code works fine on the login page but it doesn't work on the pop up login page. It is also inside iframe.

Here is the code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = Options()

option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")
option.add_experimental_option("excludeSwitches", ['enable-automation'])

# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.notifications": 1 
})
driver = webdriver.Chrome(chrome_options=option, executable_path='C:\\Users\\Sheik\\Desktop\\web crawling\\chromedriver.exe')

driver.get('https://www.reddit.com')

# clicking on the login button
login_btn = driver.find_element_by_xpath('//*[@id="SHORTCUT_FOCUSABLE_DIV"]/div[1]/header/div/div[2]/div[2]/div[1]/a[1]')
login_btn.click()

# calling the iFrame page
frame = driver.find_element_by_class_name('_25r3t_lrPF3M6zD2YkWvZU')
driver.switch_to.frame(frame)

# sending the username 
username_field = driver.find_element_by_id('loginUsername')
username_field.click()
username_field.send_keys('test_username')

# sending the password
password_field = driver.find_element_by_id('loginPassword')
password_field.click()
password_field.send_keys('test_pass')

# clicking the login submit button
submit_btn = driver.find_element_by_class_name('AnimatedForm__submitButton')
submit_btn.click()

You need to deal with the fieldset while handling elements on the iframe. Please find below working solution:

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


driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
driver.get('https://www.reddit.com')

# clicking on the login button
login_btn = driver.find_element_by_xpath('//*[@id="SHORTCUT_FOCUSABLE_DIV"]/div[1]/header/div/div[2]/div[2]/div[1]/a[1]')
login_btn.click()



driver.implicitly_wait(10)
page_title = driver.title


frame = driver.find_element_by_xpath("//iframe[@class='_25r3t_lrPF3M6zD2YkWvZU']")
driver.switch_to.frame(0)


print("alert accepted")
driver.implicitly_wait(10)

username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//fieldset//input[@id='loginUsername']")))
username.send_keys('test_username')

pwd = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//fieldset//input[@id='loginPassword']")))
pwd.send_keys('password')

login_btn = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//fieldset//button[@class='AnimatedForm__submitButton']")))
login_btn.click()

Output:

在此处输入图像描述

Try this:

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of((By.ID, 'loginUsername')))
username_field = driver.find_element_by_id('loginUsername')
username_field.send_keys('test_username')

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