我该怎么做才能在Python代码中修复NoSuchElementException错误?

时间:2018-06-29 04:34:23

标签: python python-3.x selenium-webdriver

我正在尝试进入特定链接(在代码中给出)并使用用户ID和密码登录。登录后,我想单击一个名为“查看所有xxx评论”的按钮。我正在使用硒来完成这项任务。不幸的是,我说错了。

**追踪(最近通话):   文件“ C:/Users/xxxx/.PyCharmEdu2018.1/config/scratches/scratch_2.py”,第16行,在     username_box = driver.find_element_by_id('emailInput-input')

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“ method”:“ id”,“ selector”:“ emailInput-input”}   (会议信息:chrome = 67.0.3396.99)   (驱动程序信息:chromedriver = 2.40.565498(ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform = Windows NT 10.0.17134 x86_64)**

这是我的代码:

Google Chrome:  67.0.3396.99 (Official Build) (64-bit)
Revision:       a337fbf3c2ab8ebc6b64b0bfdce73a20e2e2252b-refs/branch-heads/3396@{#790}
OS:             Linux
JavaScript:     V8 6.7.288.46
Flash:          30.0.0.113 /home/nishanth/.config/google-chrome/PepperFlash/30.0.0.113/libpepflashplayer.so
User Agent:     Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Command Line:   /usr/bin/google-chrome-stable --flag-switches-begin --flag-switches-end
Profile Path:   /home/nishanth/.config/google-chrome/Default

1 个答案:

答案 0 :(得分:0)

尝试使用其他元素标识符,例如by_xpathby_class_name。可能就像@raviraja所说的那样,该元素可能尚未加载。您已经导入了WebDriverWait,只需使用类似WebDriverWait(driver, 10).until(EC.presence_of_element_located(By.ID, 'emailInput-input'))的东西即可。

只需确保导入from selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import By