简体   繁体   English

Python中“无法找到具有xpath的元素”错误

[英]'Unable to locate an element with the xpath' error in Python

So I am trying to login into my instagram account using phantomJS. 所以我试图使用phantomJS登录到我的instagram帐户。 However I get a an error on the fourth line ("dom...") 但是我在第四行出现错误(“ dom ...”)

warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless ' Traceback (most recent call last): warnings.warn('PhantomJS的硒支持已被弃用,请使用无头'Traceback(最近一次调用):

and the traceback call concludes with selenium.common.exceptions.InvalidSelectorException: Message: errorMessage":"Unable to locate an element with the xpath expression 并且回溯调用以selenium.common.exceptions.InvalidSelectorException结尾:消息:errorMessage“:”无法使用xpath表达式定位元素

What is the cause of this error? 此错误的原因是什么?

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('https://www.instagram.com/accounts/login/')
dom = driver.find_element_by_xpath('//')
import pdb
pdb.set_trace()
username = dom.find_element_by_name('username')
password = dom.find_element_by_name('password')
login_button = dom.find_element_by_xpath('//[@class="_qv64e _gexxb _4tgw8 _njrw0"]')
username.clear()
password.clear()
username.send_keys('your username')
password.send_keys('your password')
login_button.click()
driver.get('https://www.instagram.com/accounts/login')
if 'logged-in' in driver.page_source:
  print 'Logged in'

通过xpath选择html文档中所有元素的正确方法是//*登录按钮的正确,完整xpath是//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/span[1]/button

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM