我正在尝试使用Selenium学习使用Python进行Web爬网。我正在测试Reddit.com。我被困在这里。当我运行脚本时,它会在登录页面上停止,并显示以下错误: (selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“ method”:“ css选择器”,“ 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()
答案 0 :(得分:0)
在处理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()
输出:
答案 1 :(得分:-1)
尝试一下:
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')