-1
Ich versuche, den kopflosen Browser zu verwenden. Ich erhalte jedoch einen Fehler, wenn ich versuche, einen Xpath-Selektor zu senden.Selenium PhantomJs Python kann Element nicht finden
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()
bekomme ich folgende Fehlermeldung:
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
Ich habe versucht, diesen Code zu verwenden Chrom-Browser und es funktioniert gut. Ich habe auch versucht, einen ID-Selektor zu verwenden.
Ich habe das gleiche Problem hat. Ich habe 'time.sleep' anstelle von' driver.implicitly_wait' verwendet und es hat funktioniert. – nutmeg64
Ja, ich habe das für eine andere Seite versucht und es funktioniert gut. Ich denke, es hat gerade abgelaufen –
Warum verwenden Sie XPath, um eine einfache ID zu finden? Verwenden Sie '.find_element_by_id()'. Die Fehlermeldung besagt, dass das Element nicht gefunden werden kann. Es könnte aus vielen Gründen ... der Locator ist falsch, das Element ist noch nicht verfügbar, usw. Was haben Sie versucht, es zu lokalisieren? – JeffC