簡體   English   中英

消息:沒有這樣的元素:無法找到元素:{“method”:“css selector”,“selector”:“[id=”loginUsername“]”}

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

我正在嘗試使用 Selenium 學習 Web 與 Python 一起抓取。 我正在測試 Reddit.com。 我被困在這里。 當我運行腳本時,它在登錄頁面上停止並給出以下錯誤:(selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector ":"[id="loginUsername"]"}) 請注意,相同的代碼在登錄頁面上工作正常,但在彈出的登錄頁面上不起作用。 它也在 iframe 內部。

這是代碼:

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

在處理 iframe 上的元素時,您需要處理字段集。 請找到以下工作解決方案:

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:

在此處輸入圖像描述

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{"method":"css selector","selector":"[id="events_table"]"} 收到消息:沒有這樣的元素:無法找到元素:{"method":"css selector","selector":"[id="None"]"} Python 使用 selenium 時 無法找到硒中的元素:{“方法”:“ css選擇器”,“選擇器”:“ [id =” identifierId”]”} selenium.common.exceptions.NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{“方法”:“css選擇器”,“選擇器”:“.ui流感~”} 沒有這樣的元素:無法定位元素:{“method”:“css selector”,“selector”:“[id=”guided-tour-tag-credentials-new“]”} 消息:沒有這樣的元素:無法定位元素:{“method”:“css selector”,“selector”:“[name=”uID“]”} NoSuchElementException:消息:沒有這樣的元素:無法定位元素:{“method”:“css selector”,“selector”:“.selected”} 無法定位元素:{“method”:“css selector”,“selector”:“.Mr508”} 使用 Selenium Python 選擇下拉菜單 - 無法定位元素:{“method”:“css selector”,“selector”:"[id= NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{"method":"xpath","selector":"//*[@id="my id"]"}
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM