Python Selenium 'WebDriver' object has no attribute error

纵饮孤独 提交于 2019-12-12 08:06:10

问题


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.

# -*- 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.


回答1:


  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.text to return the text of the page.

.text is a method of an element object. Step 2 is the assignment of the element to the name html.



来源:https://stackoverflow.com/questions/12909884/python-selenium-webdriver-object-has-no-attribute-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!