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