Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正确的xpath但通过xpath查找元素失败_Python_Xml_Facebook_Selenium_Xpath - Fatal编程技术网

Python 正确的xpath但通过xpath查找元素失败

Python 正确的xpath但通过xpath查找元素失败,python,xml,facebook,selenium,xpath,Python,Xml,Facebook,Selenium,Xpath,我正在搜索的元素的xpath是正确的 之前的所有元素都已定位,并已通过xpath对其采取了操作 但是,对于这个单实例位置,xpath是失败的。当我在Selenium中搜索它时,几乎是该网站主动否认它的存在 有人有过这样的经历吗?我可以被指向一个方向去学习更多关于它的知识,以及如何修复它 我正在自动化的网站是facebook.com 我的鼠标放在我试图在下图中查找并输入的表单上 相关代码如下: # from facebook homepage search search for the

我正在搜索的元素的xpath是正确的

之前的所有元素都已定位,并已通过xpath对其采取了操作

但是,对于这个单实例位置,xpath是失败的。当我在Selenium中搜索它时,几乎是该网站主动否认它的存在

有人有过这样的经历吗?我可以被指向一个方向去学习更多关于它的知识,以及如何修复它

我正在自动化的网站是facebook.com

我的鼠标放在我试图在下图中查找并输入的表单上

相关代码如下:

    # from facebook homepage search search for the group
waitForElements(['homepageSearchBarsearchButton'])  #STACK OVERFLOW CODE FAILS HERE
element = driver.find_element_by_xpath((xpath['homepageSearchField']))
element.send_keys(passedText['homepageSearchBarInput'])
time.sleep(randomWait())
element = driver.find_element_by_xpath(xpath['homepageSearchBarsearchButton'])
element.click()
等待下面的元素函数

def waitForElements(xpaths = [], *args):
elementLoad = None

for x in xpaths:
 # noinspection PyRedeclaration
 elementLoad =  WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH, xpath[x])))
故障跟踪如下:

  File "/home/honeytoes/PycharmProjects/Chicags20sEarly30s/lookingForTagCompliance/lookingForTagCompliance/workingFile.py", line 108, in <module>
        element = driver.find_element_by_xpath(xpath['groupSearchField'])
      File "/home/honeytoes/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 258, in find_element_by_xpath
        return self.find_element(by=By.XPATH, value=xpath)
      File "/home/honeytoes/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
        {'using': by, 'value': value})['value']
      File "/home/honeytoes/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
        self.error_handler.check_response(response)
      File "/home/honeytoes/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":".//*[@id='u_jsonp_5_2']/span/span/input"}
    Stacktrace:
文件“/home/honeyteos/pycharm项目/Chicags20sEarly30s/lookingForTagCompliance/lookingForTagCompliance/workingFile.py”,第108行,在
element=driver。通过xpath(xpath['groupSearchField'])查找元素
文件“/home/honeytoos/.local/lib/python2.7/site packages/selenium/webdriver/remote/webdriver.py”,第258行,在find_元素中
返回self.find_元素(by=by.XPATH,value=XPATH)
文件“/home/honeytoos/.local/lib/python2.7/site packages/selenium/webdriver/remote/webdriver.py”,第712行,在find_元素中
{'using':by'value':value})['value']
文件“/home/honeytoos/.local/lib/python2.7/site packages/selenium/webdriver/remote/webdriver.py”,执行中的第201行
self.error\u handler.check\u响应(响应)
文件“/home/honeytoos/.local/lib/python2.7/site packages/selenium/webdriver/remote/errorhandler.py”,第181行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id='u_jsonp_5_2']]/span/span/input”}
堆栈跟踪:

谢谢

我们如何基于
占位符
属性制作一个可选的可读选择器:

//input[@placholder="Search this group"]

我们如何根据
占位符
属性制作另一个可读选择器:

//input[@placholder="Search this group"]

我不能百分之百肯定你的意思。可读的选择器是我可以搜索并了解更多的术语吗?或者你能为我指出我应该去的方向,以便了解你的解决方案的细节吗?感谢you@ChristopherJakob抱歉搞混了。我的意思是尝试使用建议的xpath表达式,而不是
/*[@id='u\u jsonp\u 5\u 2']/span/span/input
。哦,我明白了,所以您调用xpath搜索带有占位符文本“Search this group”的字段。聪明的,我会尝试一下现在正在运行它,但是仍然有搜索按钮按下的问题。不过,也许我可以发送Enter键Thank you@alecxe,但是建议的修复方案不起作用,我将继续digI我不完全确定你的意思。可读的选择器是我可以搜索并了解更多的术语吗?或者你能为我指出我应该去的方向,以便了解你的解决方案的细节吗?感谢you@ChristopherJakob抱歉搞混了。我的意思是尝试使用建议的xpath表达式,而不是
/*[@id='u\u jsonp\u 5\u 2']/span/span/input
。哦,我明白了,所以您调用xpath搜索带有占位符文本“Search this group”的字段。聪明的,我会尝试一下现在正在运行它,但是仍然有搜索按钮按下的问题。不过,也许我可以发送Enter键Thank you@alecxe,但是建议的修复方案不起作用,我将继续挖掘