python中的硒:NoSuchElementException:消息:没有这样的元素:无法找到元素 [英] selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

查看:205
本文介绍了python中的硒:NoSuchElementException:消息:没有这样的元素:无法找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在ID的第一块中键入"abc",在密码的第二块中键入"cdef". 但是,底部出现了错误代码.

from selenium import webdriver
driver.get('http://sugang.korea.ac.kr')

添加了隐式等待,以防止代码在页面完全加载之前执行.

driver.implicitly_wait(30)

添加用户名和密码的代码如下

driver.find_element_by_name('id').send_keys('abc') driver.find_element_by_name('pw').send_keys('cdef')

但是低于错误

NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"id","selector":"id"}

请.帮帮我^^

解决方案

用户名密码字段位于frame之内,因此您必须:

  • 诱导 WebDriverWait 以使所需的框架可用并切换到.
  • 诱导 WebDriverWait 以使所需的元素可点击.
  • 您可以使用以下解决方案:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe')
    driver.get("http://sugang.korea.ac.kr")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc')
    driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")
    

  • 浏览器快照:

I tried typing 'abc' in the first block of id and 'cdef' in the second block of password. However, the error code at the bottom comes up.

from selenium import webdriver
driver.get('http://sugang.korea.ac.kr')

Added implicitly wait to prevent the code from executing before the page fully loads.

driver.implicitly_wait(30)

Code for adding username and password is as below

driver.find_element_by_name('id').send_keys('abc') driver.find_element_by_name('pw').send_keys('cdef')

But getting below error

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"id"}

please. help me ^^

解决方案

The username and password fields are within an frame, so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe')
    driver.get("http://sugang.korea.ac.kr")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc')
    driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")
    

  • Browser Snapshot:

这篇关于python中的硒:NoSuchElementException:消息:没有这样的元素:无法找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆