0

I am just starting with selenium.

I made a simple python script that should open a url and print the price of a product there.

Here it is:

from selenium import webdriver
import time
driver = webdriver.PhantomJS(executable_path='/usr/bin/phantomjs')
url = 'http://www.stance.com/shop/product/paint-trap'
print "Driver Made"
driver.get(url)
print "URL got"
price = driver.find_element_by_xpath('//*[@id="h1--title-price"]/span[2]').text
print price
driver.close()

But, it just prints: "Driver Made" and never prints "URL got" nor the price.

It seems to be getting stuck on the driver.get(url), but I don't know why.

I would like to know how to print the price and how to stop driver.get(url) from running eternally.

If I interrupt it with a ctrl C, i get:

Driver Made
^CTraceback (most recent call last):
  File "test.py", line 6, in <module>
    driver.get(url)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 213, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 199, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 463, in _request
    resp = opener.open(request, timeout=self._timeout)
  File "/usr/local/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/local/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/usr/local/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python2.7/urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/local/lib/python2.7/urllib2.py", line 1187, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/local/lib/python2.7/httplib.py", line 1045, in getresponse
    response.begin()
  File "/usr/local/lib/python2.7/httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python2.7/httplib.py", line 365, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/local/lib/python2.7/socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
8
  • you are priniting it -> 7 line -> print "URL got" Commented Oct 14, 2015 at 9:00
  • "URL got" is never printing, because get(url) runs eternally. I want to know why get(url) is running eternally and how to stop it. @ShubhamJain Commented Oct 14, 2015 at 9:03
  • $14.00 is the result i got.The code is working correctly i guess Commented Oct 14, 2015 at 9:08
  • weird, thanks for running it @vks are you also using phantomjs? Commented Oct 14, 2015 at 9:09
  • @Rorschach no firefox .... check if phantom is supported Commented Oct 14, 2015 at 9:10

1 Answer 1

1

Working code prints- (in windows).

Driver Made
URL got
$14.00

Working code as below

from selenium import webdriver
import time
driver = webdriver.PhantomJS(executable_path=r"C:\Users\Desktop\phantomjs.exe")

driver.set_window_size(1120, 550)
url = 'http://www.stance.com/shop/product/paint-trap'
print "Driver Made"
driver.get(url)
print "URL got"
driver.implicitly_wait(5)
price = driver.find_elements_by_xpath("(//*[@id='h1--title-price']/span)[2]")
for i in price:
    print i.text
driver.close()

N.B. Make sure phantom executable path and selenium library is correct in other oses.

Sign up to request clarification or add additional context in comments.

2 Comments

Sadly, mine still runs eternally with this. It must be some error that doesn't have to do with the code, I am gonna try reinstalling phantomJS and if that doesn't work I'll try firefox
Thank you for affirming that the code works, I will mark your answer as correct, since the code works and that is what I was asking for. I think my ubuntu was just running extraordinarily slow. So, I will go about fixing that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.