2017-08-17 57 views
-1

我正在尝试使用无头浏览器。但是,当我尝试将.sendkeys添加到xpath选择器时,我不断收到错误。Selenium PhantomJs Python无法找到元素

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



driver=webdriver.PhantomJS("C:\\Users\\ikhan\\AppData\\Local\\Programs\\Python\\Python36-32\\selenium\\webdriver\\phantomjs.exe") 
driver.maximize_window() 
driver.set_page_load_timeout(30) 
driver.implicitly_wait(20) 
driver.get("https://google.com") 
driver.find_element_by_xpath('''//*[@id="lst-ib"]''').send_keys("anish") 
driver.find_element_by_xpath('''//*[@id="lst-ib"]''').send_keys(Keys.RETURN) 

driver.close() 

我得到以下错误:

Traceback (most recent call last): 
    File "C:/Users/ikhan/Desktop/SoftsystemSolutions/SupremeBot/headless.py", line 16, in <module> 
    driver.find_element_by_xpath('''//*[@id="lst-ib"]''').send_keys(Keys.RETURN) 
    File "C:\Users\ikhan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 313, in find_element_by_xpath 
    return self.find_element(by=By.XPATH, value=xpath) 
    File "C:\Users\ikhan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 791, in find_element 
    'value': value})['value'] 
    File "C:\Users\ikhan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute 
    self.error_handler.check_response(response) 
    File "C:\Users\ikhan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with xpath '//*[@id=\"lst-ib\"]'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"103","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:64743","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"value\": \"//*[@id=\\\"lst-ib\\\"]\", \"sessionId\": \"c7193fb0-8381-11e7-80d0-b324a0b7e579\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/c7193fb0-8381-11e7-80d0-b324a0b7e579/element"}} 
Screenshot: available via screen 

我曾尝试使用Chrome浏览器使用此代码和它工作得很好。我也尝试使用ID选择器。

+0

我有同样的问题。我使用'time.sleep'而不是'driver.implicitly_wait',它工作。 – nutmeg64

+0

是的,我试过这个不同的网站,它工作正常。我认为它只是超时 –

+0

为什么你使用XPath找到一个简单的ID?使用'.find_element_by_id()'。错误消息指出无法找到该元素。这可能是由于很多原因...定位器是错误的,元素尚不可用,等等。你试图找到它? – JeffC

回答

0

要找到谷歌搜索框使用name定位如下:

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

driver = webdriver.PhantomJS(executable_path="C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe") 
driver.get("https://google.com") 
google_search = driver.find_element_by_name("q") 
google_search.send_keys("anish") 
google_search.send_keys(Keys.RETURN) 
print("Google Search Complete")