Python Selenium 'WebDriver' 对象没有属性错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12909884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:09:19  来源:igfitidea点击:

Python Selenium 'WebDriver' object has no attribute error

pythonseleniumattributesweb-scrapingattributeerror

提问by DannyMatt

I'm trying to scrape some javascript-generated content from a Chinese-language website. I'm using Selenium (and Python) since I can't scrape the javascript content directly.

我正在尝试从中文网站抓取一些 javascript 生成的内容。我正在使用 Selenium(和 Python),因为我无法直接抓取 javascript 内容。

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.selenium import selenium 

import time
import urllib2
import httplib
import urllib
import re
import base64

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www...") # Load page (redacted here, but any works)
browser.get_body_text() #Attempt to scrape body text

And I get the following error:

我收到以下错误:

'WebDriver' object has no attribute 'get_body_text'

In fact, I can't seem to call any commands in the selenium.selenium class. No doubt I'm overlooking something very obvious. Thanks in advance.

事实上,我似乎无法在 selenium.selenium 类中调用任何命令。毫无疑问,我忽略了一些非常明显的东西。提前致谢。

采纳答案by kreativitea

  1. You only need from selenium import webdriver.
  2. Execute html= browser.find_element_by_xpath(".//html")to get the html element on the page, the largest element. (You can do this any number of ways, and select any number of elements. )
  3. Execute html.textto return the text of the page.
  1. 你只需要from selenium import webdriver.
  2. 执行html= browser.find_element_by_xpath(".//html")获取页面上的html元素,最大的元素。(您可以通过多种方式执行此操作,并选择任意数量的元素。)
  3. 执行html.text以返回页面的文本。

.textis a method of an elementobject. Step 2 is the assignment of the elementto the name html.

.text是一个element对象的方法。第 2 步是将 分配element给 name html