正确的xpath但find_element_by_xpath失败了python

时间:2015-12-22 02:47:59

标签: python xml facebook selenium xpath

我要搜索的元素的xpath是正确的。

找到之前的所有元素,并通过xpath对它们采取了行动。

但是对于这个实例,xpath的位置失败了。几乎如果我在Selenium搜索它时网站正在积极否认它的存在。

有没有人有这方面的经验?我是否可以指明一个方向来了解它,以及如何解决它。

我自动化的网站是facebook.com

我的鼠标在我尝试查找并输入到下图中的表格上方 No I am not worried about you seeing my facebook

相关代码如下:

    # 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:

谢谢

1 个答案:

答案 0 :(得分:0)

我们如何基于placeholder属性制作替代可读选择器:

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