如何用Selenium找到物体?

时间:2013-05-16 08:44:34

标签: python selenium python-2.7

这是我的代码,我正在改变,修复,尝试另一个模块,但仍然无法得到我的div文本提取的响应。

import selenium 
from selenium import webdriver

driver = webdriver.Firefox() 
driver.get('http://www.helloworld.com/')
element = driver.find_element_by_id('main')

WebElement = driver.findElement(By.xpath("//div[@class='main']"));
webElement.getText();

我正在尝试使用bs4软件包,但是存在一个很大的问题,因为我想要的数据只有当我在网站上时才能获得,并且在bs4中响应就像来自访客帐户,而无需登录。

这是一个Traceback我得到的代码是Selenium:

Traceback (most recent call last):
  File "D:/Python27/get text value div.py", line 8, in <module>
    WebElement = driver.findElement(By.xpath("//div[@class='main']"));
AttributeError: 'WebDriver' object has no attribute 'findElement'

Aftter一个小修复,我正在使用它:

import selenium 
from selenium import webdriver

driver = webdriver.Firefox() 
driver.get('http://www.helloworld.com/')
element = driver.find_element_by_id('main')
main_text = element.text

shell中print element.text之后的回复是:

追踪(最近一次呼叫最后一次):

  File "<pyshell#20>", line 1, in <module>
    element.text
  File "D:\Python27\lib\selenium\webdriver\remote\webelement.py", line 50, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "D:\Python27\lib\selenium\webdriver\remote\webelement.py", line 228, in _execute
    return self._parent.execute(command, params)
  File "D:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "D:\Python27\lib\selenium\webdriver\remote\errorhandler.py", line 152, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'\'[JavaScript Error: "a is null" {file: "file:///c:/dokume~1/tomek/lokale%20einstellungen/temp/tmpupvgr2/extensions/fxdriver@googlecode.com/components/command_processor.js" line: 7623}]\' when calling method: [nsICommandProcessor::execute]' 

2 个答案:

答案 0 :(得分:8)

你从哪里得到以下内容?看起来像Java。

 WebElement = driver.findElement(By.xpath("//div[@class='main']"));

webElement.getText();

尝试:

import selenium 
from selenium import webdriver

driver = webdriver.Firefox() 
driver.get('http://www.helloworld.com/')
element = driver.find_element_by_id('main')
print element.text #There's no text under div main, what would you expect?

footer = driver.find_element_by_id('footer')
print footer.text
# Should print out "Copyright ©2013 helloworld.com. All Rights Reserved. About Us   |  Privacy Policy "

答案 1 :(得分:0)

如果您想要的信息位于ID为botloc的div中,那么您需要获取该元素。

import selenium 
from selenium import webdriver

driver = webdriver.Firefox() 
driver.get('http://www.helloworld.com/')
bot_location = driver.find_element_by_id('botloc').text
print bot_location