无法定位元素没有这样的元素

时间:2017-12-12 04:06:48

标签: python-3.x selenium-webdriver web-scraping scrapy

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException


driver = webdriver.Chrome()
driver.get('http://www.techfetch.com/JS/dashboard.aspx?page=fetchjobs')
time.sleep(5)
element = driver.find_element_by_xpath('//*[@id="txtKeyword"]')
element.send_keys("Java")

错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="txtKeyword"]"}
  (Session info: chrome=62.0.3202.94)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64)

enter image description here

屏幕截图显示xpath正确且可用

2 个答案:

答案 0 :(得分:2)

因为您尝试填写的输入字段位于iframe之内。首先切换它,然后尝试做你之前做的事情:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('http://www.techfetch.com/JS/dashboard.aspx?page=fetchjobs')
time.sleep(5)
driver.switch_to_frame(driver.find_element_by_id("contentframe"))  ##switch it
driver.find_element_by_xpath('//*[@id="txtKeyword"]').send_keys("Java") 

答案 1 :(得分:0)

要将字符序列发送到Keywords框,首先我们需要waitframe可以切换到它,然后找到Keywords框到发送字符序列如下:

driver.get('http://www.techfetch.com/JS/dashboard.aspx?page=fetchjobs')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"//iframe[@id='contentframe']"))
element = driver.find_element_by_xpath("//input[@id='txtKeyword']")
element.send_keys("Java")