Selenium脚本不显示XPath值

时间:2018-12-20 15:52:47

标签: python selenium

我制作了这个Selenium脚本,作为刮擦JS重磅页面的一种做法。

程序,启动WebDriver进入网站,然后按一个按钮,这样它们全部显示出来,然后我只想提取一些数据,俱乐部的名称,但是有问题。

它只是打印[],有人可以告诉我我在做什么错吗?

我的目标是获得像Acadiana Kennel Club,Inc.这样的俱乐部的名称

def _execute_query(self):
    connection = connections[self.using]

    # Adapt parameters to the database, as much as possible considering
    # that the target type isn't known. See #17755.
    params_type = self.params_type
    adapter = connection.ops.adapt_unknown_value
    if params_type is tuple:
        params = tuple(adapter(val) for val in self.params)
    elif params_type is dict:
        params = {key: adapter(val) for key, val in self.params.items()}
    else:
        raise RuntimeError("Unexpected params type: %s" % params_type)

    self.cursor = connection.cursor()
    self.cursor.execute(self.sql, params)

1 个答案:

答案 0 :(得分:2)

'//*[@class="ng-binding"]/text()' Selenium不支持XPath语法,因为XPath只能返回WebElement。

尝试以下

buttonXpath = '//a[@class="button" and @name="Search"]'
namesXpath = '//a/strong[@class="ng-binding"]'

try:
    buttonElement = WebDriverWait(browser, timeout).until(lambda browser: browser.find_element_by_xpath(buttonXpath))
    buttonElement.click()
    clubNames = [club.text for club in WebDriverWait(browser, timeout).until(lambda browser: browser.find_elements_by_xpath(namesXpath))]
    for clubName in clubNames: 
        print(clubName)
except TimeoutException:
    print('Timed out waiting for page to load')
    browser.quit()