Selenium PhantomJs Python无法找到元素

时间:2017-08-17 19:31:54

标签: python selenium phantomjs

我试图使用无头浏览器。但是,当我尝试.sendkeys到xpath选择器时,我一直收到错误。

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选择器。

1 个答案:

答案 0 :(得分:0)

要找到Google搜索框,请按以下方式使用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")