Selenium WebDriver如何在javascript中验证来自Span Tag的Text

时间:2017-05-02 12:56:30

标签: javascript selenium

我无法从span标记中获取值,如下所示:

var error = driver.findElement(webdriver.By.id('error-container-text')).getAttribute('innerHTML');

但这显示出类似的内容 -

ManagedPromise {
  flow_: 
   ControlFlow {
     propagateUnhandledRejections_: true,
     activeQueue_: 
      TaskQueue {
        name_: 'TaskQueue::216',
        flow_: [Circular],
        tasks_: [Object],
        interrupts_: null,
        pending_: null,
        subQ_: null,
        state_: 'new',
        unhandledRejections_: Set {} },
     taskQueues_: Set { [Object] },
     shutdownTask_: null,
     hold_: 
      Timeout {
        _called: false,
        _idleTimeout: 2147483647,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 22977,
        _onTimeout: [Function],
        _timerArgs: undefined,
        _repeat: 2147483647 } },
  stack_: 
   { Task: WebElement.getAttribute(innerHTML)
       at thenableWebDriverProxy.schedule (/home/app/node_modules/selenium-webdriver/lib/webdriver.js:816:17)
       at WebElementPromise.schedule_ (/home/app/node_modules/selenium-webdriver/lib/webdriver.js:2024:25)
       at WebElementPromise.getAttribute (/home/app/node_modules/selenium-webdriver/lib/webdriver.js:2276:17)
       at Context.<anonymous> (/home/app/PycharmProjects/SampleTest/Sample.js:72:85)
       at runTest (/home/app/node_modules/selenium-webdriver/testing/index.js:164:22)
       at /home/app/node_modules/selenium-webdriver/testing/index.js:185:16
       at new ManagedPromise (/home/app/node_modules/selenium-webdriver/lib/promise.js:1085:7)
       at controlFlowExecute (/home/app/node_modules/selenium-webdriver/testing/index.js:184:14)
       at TaskQueue.execute_ (/home/app/node_modules/selenium-webdriver/lib/promise.js:3092:14)
       at TaskQueue.executeNext_ (/home/app/node_modules/selenium-webdriver/lib/promise.js:3075:27)
       at asyncRun (/home/app/node_modules/selenium-webdriver/lib/promise.js:2982:25)
       at /home/app/node_modules/selenium-webdriver/lib/promise.js:676:7 name: 'Task' },
  parent_: null,
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  value_: undefined,
  queue_: null }

1 个答案:

答案 0 :(得分:0)

尝试使用以下内容:

import requests
import urllib.request
from lxml import html

def PictureScraping():
    url = "https://www.yify-torrent.org/search/1080p/"
    response = requests.get(url)
    tree = html.fromstring(response.text)
    titles = tree.xpath('//div[@class="movie-image"]')
    for title in titles:
        Pics = "https:" + title.xpath('.//img/@src')[0]
        urllib.request.urlretrieve(Pics, Pics.split('/')[-1])
PictureScraping()