Mezulith
Mezulith

Reputation: 33

How can I find object with Selenium?

Thats my code, I was changing, fixing, trying another modules, but still can not get a respond with my div text extracted.

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();

I was trying with bs4 package but there is a big problem, becouse the data what i want is possible to get only when i am loged in on the website, and in bs4 respond was like from guest account, without login in.

Here is a Traceback what i got usint that code with 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 a small fix, I am using this:

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

The respond after print element.text in shell is:

Traceback (most recent call last):

  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/[email protected]/components/command_processor.js" line: 7623}]\' when calling method: [nsICommandProcessor::execute]' 

Upvotes: 1

Views: 13301

Answers (2)

Yi Zeng
Yi Zeng

Reputation: 32905

Where did you get the following from? Looks like Java to me.

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

webElement.getText();

Try:

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 "

Upvotes: 8

aychedee
aychedee

Reputation: 25639

If the information you want is inside a div with an id of botloc then you need to grab that element.

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

Upvotes: 0

Related Questions